Re: Global Variables/Passing Data Using GUIDE



"Adam " <abc5@xxxxxx> wrote in message <htp678$77o$1@xxxxxxxxxxxxxxxxxx>...
"Matt Fig" <spamanon@xxxxxxxxx> wrote in message <htp2iu$g8g$1@xxxxxxxxxxxxxxxxxx>...
> Again, look at previous answers. The functions do not need an END statement to have and end. Put all of this into a single file, save it then call it from the command line with two integer input arguments.
> > > function B = func(a,c)
> % Non-nested functions DO NOT need an END statement.
> if a>c
> B = subfunc(a,c);
> else
> B = subfunc2(a,c);
> end
> > function B = subfunc(a,c)
> B = a + c;
> > function B = subfunc2(a,c)
> B = a-c;

Ok thanks, my problem is not so much with the end statements but rather with defining a variable in a subfunction (that happens not to end-it is a callback for a menu item) and using it within another function. I know this can somehow be done using handles but I'm not sure how. Global variables don't work.

The behavior I'm looking for is for this menu item to ask the user to choose a file via uigetfile which returns the filename as string. Then use that string in my start button callback to read the file and continue.


In your GUI OpeningFcn do:

setappdata(0,'HandleAudioDemo',hObject)

That will save the handle for the GUI to your matlab "base".

Then you can get the handle in other functions by doing:

HandleGUI=getappdata(0,'HandleAudioDemo');

And finally you can save and get data by doing:

setappdata(HandleGUI,'MyVariable',4); % to save data to a variable called MyVariable

or to get data:

MyVariable=getappdata(HandleGUI,'MyVariable'); %to get your variable data to a local variable.

Best regards
.



Relevant Pages