Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Global Variables/Passing Data Using GUIDE

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 17:11:11

Message: 1 of 17

Hi,

I'm creating a somewhat complex GUI and will need to pass data to and from functions clearly and easily. Because of the complexity of the project, I would like to use GUIDE to make the GUI programming simpler.

The issue I'm having is with GUIDE's generated .m file. In the auto generated format, functions don't seem to have an end command. The automatic generated function is open and callbacks also don't end. If I properly end functions, the code doesn't run.

I would like to define variables that can be accessed within callbacks. For example, one button asks the user to choose a file, while another uses the filename to read the file and uses data from within it. Unfortunately, because the functions don't end, I can't use global variables. I also can't use the handle of the button, since no data is actually stored in a button object. I would like to do the same thing with a menu item which asks the user to import a file. Is there a simple way to pass data from within these endless functions? Is there a better way to do this?

Thanks a lot.
Adam

Subject: Global Variables/Passing Data Using GUIDE

From: Steven Lord

Date: 28 May, 2010 17:40:08

Message: 2 of 17


"Adam " <abc5@ubc.ca> wrote in message
news:htotfe$p06$1@fred.mathworks.com...
> Hi,
>
> I'm creating a somewhat complex GUI and will need to pass data to and from
> functions clearly and easily. Because of the complexity of the project, I
> would like to use GUIDE to make the GUI programming simpler.
>
> The issue I'm having is with GUIDE's generated .m file. In the auto
> generated format, functions don't seem to have an end command.

That's correct.

> The automatic generated function is open and callbacks also don't end.

I don't know what you mean by "is open" in this context, but you are not
correct that the callback functions don't end. Any function in a file that
contains a nested function must end with an END, and each such function will
end at its corresponding END; functions in a file that does not contain a
nested function should not end with END, but will end at the start of the
next function or at the end of the file.

> If I properly end functions, the code doesn't run.

Off the top of my head, I can't think of anything inherent about the
GUIDE-generated code that would prevent it from running if you made the
functions end with END -- what error message did it give you?

> I would like to define variables that can be accessed within callbacks.
> For example, one button asks the user to choose a file, while another uses
> the filename to read the file and uses data from within it. Unfortunately,
> because the functions don't end, I can't use global variables. I also
> can't use the handle of the button, since no data is actually stored in a
> button object. I would like to do the same thing with a menu item which
> asks the user to import a file. Is there a simple way to pass data from
> within these endless functions? Is there a better way to do this?

Read the "Managing and Sharing Application Data in GUIDE" section of the
Creating Graphical User Interfaces documentation.

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f5-998197.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Global Variables/Passing Data Using GUIDE

From: Walter Roberson

Date: 28 May, 2010 17:43:20

Message: 3 of 17

Steven Lord wrote:
> Any function in a file that
> contains a nested function must end with an END, and each such function will
> end at its corresponding END; functions in a file that does not contain a
> nested function should not end with END, but will end at the start of the
> next function or at the end of the file.

Steve, is there reason to avoid using END when there is no nested function?

Subject: Global Variables/Passing Data Using GUIDE

From: Matt Fig

Date: 28 May, 2010 18:17:27

Message: 4 of 17

There is nothing "proper" about ending subfunctions with an END. There is also nothing improper about ending a subfunction with an END. Neither the main function, nor the subfunctions require an END.

For example, type

>> edit nchoosek

and look at how TMW coded the file. No END statements at the ends of the subfunctions there!

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 18:21:25

Message: 5 of 17

"Steven Lord" <slord@mathworks.com> wrote in message <htov5a$aq0$1@fred.mathworks.com>...
>
> "Adam " <abc5@ubc.ca> wrote in message
> news:htotfe$p06$1@fred.mathworks.com...
> > Hi,
> >
> > I'm creating a somewhat complex GUI and will need to pass data to and from
> > functions clearly and easily. Because of the complexity of the project, I
> > would like to use GUIDE to make the GUI programming simpler.
> >
> > The issue I'm having is with GUIDE's generated .m file. In the auto
> > generated format, functions don't seem to have an end command.
>
> That's correct.
>
> > The automatic generated function is open and callbacks also don't end.
>
> I don't know what you mean by "is open" in this context, but you are not
> correct that the callback functions don't end. Any function in a file that
> contains a nested function must end with an END, and each such function will
> end at its corresponding END; functions in a file that does not contain a
> nested function should not end with END, but will end at the start of the
> next function or at the end of the file.
>
> > If I properly end functions, the code doesn't run.
>
> Off the top of my head, I can't think of anything inherent about the
> GUIDE-generated code that would prevent it from running if you made the
> functions end with END -- what error message did it give you?
>
> > I would like to define variables that can be accessed within callbacks.
> > For example, one button asks the user to choose a file, while another uses
> > the filename to read the file and uses data from within it. Unfortunately,
> > because the functions don't end, I can't use global variables. I also
> > can't use the handle of the button, since no data is actually stored in a
> > button object. I would like to do the same thing with a menu item which
> > asks the user to import a file. Is there a simple way to pass data from
> > within these endless functions? Is there a better way to do this?
>
> Read the "Managing and Sharing Application Data in GUIDE" section of the
> Creating Graphical User Interfaces documentation.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f5-998197.html
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

What I meant by is open, is simply that it does not end. I'm having numerous errors when ending the callbacks. What happens is, if I end each callback, function varargout also requires a callback. If I end varargout, it runs, but gives a list of errors as shown below (there's more- this is a sample). Essentially, I am ending the Guide Generated Code in this case.

??? Error using ==> feval
Undefined function or method 'nContoursBox_CreateFcn' for input
arguments of type 'double'.

Error in ==> gui_mainfcn at 96
        feval(varargin{:});

Error in ==> optip at 26
    gui_mainfcn(gui_State, varargin{:});

Error in ==>
guidemfile>@(hObject,eventdata)optip('nContoursBox_CreateFcn',hObject,eventdata,guidata(hObject))


??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn

??? Error using ==> feval
Undefined function or method 'nMaxIterationsBox_CreateFcn' for input
arguments of type 'double'.

Error in ==> gui_mainfcn at 96
        feval(varargin{:});

Error in ==> optip at 26
    gui_mainfcn(gui_State, varargin{:});

Error in ==>
guidemfile>@(hObject,eventdata)optip('nMaxIterationsBox_CreateFcn',hObject,eventdata,guidata(hObject))


??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn

??? Error using ==> feval
Undefined function or method 'regAccuracyBox_CreateFcn' for input
arguments of type 'double'.

Subject: Global Variables/Passing Data Using GUIDE

From: Matt Fig

Date: 28 May, 2010 18:38:22

Message: 6 of 17

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;

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 19:40:24

Message: 7 of 17

"Matt Fig" <spamanon@yahoo.com> wrote in message <htp2iu$g8g$1@fred.mathworks.com>...
> 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.

Subject: Global Variables/Passing Data Using GUIDE

From: Paulo

Date: 28 May, 2010 20:05:25

Message: 8 of 17

"Adam " <abc5@ubc.ca> wrote in message <htp678$77o$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <htp2iu$g8g$1@fred.mathworks.com>...
> > 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

Subject: Global Variables/Passing Data Using GUIDE

From: Matt Fig

Date: 28 May, 2010 20:19:19

Message: 9 of 17

Wherever the string is returned from uigetfile, save it using guidata or something similar. Then in your start button callback, access the string.

% in menuitem callback
STR = uigetfile(...)
HC = guidata(gcbf);
HC.STR = STR;
guidata(gcbf,HC)


% Now in your start button callback
HC = guidata(gcbf)
STR = HC.STR; % This is the string returned by gui

Subject: Global Variables/Passing Data Using GUIDE

From: Steven Lord

Date: 28 May, 2010 20:33:18

Message: 10 of 17


"Adam " <abc5@ubc.ca> wrote in message
news:htp1j5$dkb$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <htov5a$aq0$1@fred.mathworks.com>...

*snip*

> What I meant by is open, is simply that it does not end. I'm having
> numerous errors when ending the callbacks. What happens is, if I end each
> callback, function varargout also requires a callback. If I end varargout,
> it runs, but gives a list of errors as shown below (there's more- this is
> a sample). Essentially, I am ending the Guide Generated Code in this case.

I think we may be experiencing the problem where "end" is being overloaded
and it's ambiguous which overload is being used [and I'm not talking about
the MATLAB keyword but the English word here.]

Can you show one of your shorter callback functions that "does not end", and
explain in a bit more depth what you're actually doing when you "ending the
callbacks" or "end varargout"?

Anyway, for sharing data among callbacks, I recommend you look at one of the
techniques (like using the UserData property or the handles structure)
described on that documentation page to which I linked in my previous
message.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 20:34:18

Message: 11 of 17

Thanks, I appreciate the help. Unfortunately, this also didn't work since my functions are within the GUI. I think I'm doing a bad job of explaining my question. Here is some code from my GUI that may help illustrate my question.

I created the GUI in Guide with some menus (so far: just file and help). Since this is an optimization tool, one option will be to import objectives equations from a separate file. To do this, I've created a menu option called import_objectives. The callback is below:

    function import_objectives_Callback(hObject, eventdata, handles)
    objectivesfName=uigetfile('*.txt','Select the Text-file Containing the Objectives Functions');

This returns the file name to a string which I would like to later use in another callback (the start button). And then use this name to read a file.

function startButton_Callback(hObject, eventdata, handles, objectivesfName)
    objFuncs = textread(objectivesfName,'%s');

The problem is when this runs objectivesfName doesn't exist in startButton_Callback, even its made global since. So, I was wondering if I could create some kind of variable/handle/object either within import_objectives_callback or outside of it that allow me to access it in startButton_Callback.

Thanks a lot for the responses so far. I've tried them but can't figure it out :(. What is the best way to do this?

Subject: Global Variables/Passing Data Using GUIDE

From: Steven Lord

Date: 28 May, 2010 20:37:47

Message: 12 of 17


"Walter Roberson" <roberson@hushmail.com> wrote in message
news:ZgTLn.22588$Gx2.19637@newsfe20.iad...
> Steven Lord wrote:
>> Any function in a file that contains a nested function must end with an
>> END, and each such function will end at its corresponding END; functions
>> in a file that does not contain a nested function should not end with
>> END, but will end at the start of the next function or at the end of the
>> file.
>
> Steve, is there reason to avoid using END when there is no nested
> function?

Off the top of my head, I can't think of one. [Because I wrote this, I give
it half an hour before someone stops by my office and reminds me of such a
reason :]

If you're writing a new file and you start out ENDing all the functions I
don't think it would hurt anything. But remember, if you have a file that
contains hundreds or thousands of functions that do not end with END, and
you introduce _one_ new function that does, you have to go back to _all_ of
the functions in the file and insert the END.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 20:41:22

Message: 13 of 17

"Steven Lord" <slord@mathworks.com> wrote in message <htp9ic$95b$1@fred.mathworks.com>...
>
> "Walter Roberson" <roberson@hushmail.com> wrote in message
> news:ZgTLn.22588$Gx2.19637@newsfe20.iad...
> > Steven Lord wrote:
> >> Any function in a file that contains a nested function must end with an
> >> END, and each such function will end at its corresponding END; functions
> >> in a file that does not contain a nested function should not end with
> >> END, but will end at the start of the next function or at the end of the
> >> file.
> >
> > Steve, is there reason to avoid using END when there is no nested
> > function?
>
> Off the top of my head, I can't think of one. [Because I wrote this, I give
> it half an hour before someone stops by my office and reminds me of such a
> reason :]
>
> If you're writing a new file and you start out ENDing all the functions I
> don't think it would hurt anything. But remember, if you have a file that
> contains hundreds or thousands of functions that do not end with END, and
> you introduce _one_ new function that does, you have to go back to _all_ of
> the functions in the file and insert the END.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>
Good point, I may be missing an 'end' somewhere I'll check to make sure.

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 20:46:24

Message: 14 of 17

"Matt Fig" <spamanon@yahoo.com> wrote in message <htp8g7$26k$1@fred.mathworks.com>...
> Wherever the string is returned from uigetfile, save it using guidata or something similar. Then in your start button callback, access the string.
>
> % in menuitem callback
> STR = uigetfile(...)
> HC = guidata(gcbf);
> HC.STR = STR;
> guidata(gcbf,HC)
>
>
> % Now in your start button callback
> HC = guidata(gcbf)
> STR = HC.STR; % This is the string returned by gui

I think this is the correct approach. However I get an error when trying this saying.

??? Reference to non-existent field 'STR'.

Is defining HC.STR supposed to create a new field? Should I make an invisible one somehow?

Subject: Global Variables/Passing Data Using GUIDE

From: Matt Fig

Date: 28 May, 2010 20:53:21

Message: 15 of 17

Did you make sure that the one callback is called (the one which calls uigetfile) first before the start button callback? This works in a GUIDE GUI I created to test it. Of course if the callback order is wrong, then there will be no STR field.

One way to avoid this is to create the start button with the 'enable' property set to 'inactive'. Then once a file is chosen by the uimenu callback set the 'enable' property of the startbutton to 'on' when the STR field is added.

Subject: Global Variables/Passing Data Using GUIDE

From: Steven Lord

Date: 28 May, 2010 21:01:08

Message: 16 of 17


"Adam " <abc5@ubc.ca> wrote in message
news:htp9pi$mic$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <htp9ic$95b$1@fred.mathworks.com>...

*snip*

> Good point, I may be missing an 'end' somewhere I'll check to make sure.

If you have this code open in the MATLAB Editor, look at the upper-right
corner. Is the square there green, orange, or red? If some but not all of
your functions end in END, it will be red and there will be a red line in
the gutter below it that when you hover the mouse over it should indicate
that there's a missing END.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Global Variables/Passing Data Using GUIDE

From: Adam

Date: 28 May, 2010 21:14:21

Message: 17 of 17

"Steven Lord" <slord@mathworks.com> wrote in message <htpau6$4dr$1@fred.mathworks.com>...
>
> "Adam " <abc5@ubc.ca> wrote in message
> news:htp9pi$mic$1@fred.mathworks.com...
> > "Steven Lord" <slord@mathworks.com> wrote in message
> > <htp9ic$95b$1@fred.mathworks.com>...
>
> *snip*
>
> > Good point, I may be missing an 'end' somewhere I'll check to make sure.
>
> If you have this code open in the MATLAB Editor, look at the upper-right
> corner. Is the square there green, orange, or red? If some but not all of
> your functions end in END, it will be red and there will be a red line in
> the gutter below it that when you hover the mouse over it should indicate
> that there's a missing END.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>
You guys have been awesome. It now works. It was actually a combination of both your suggestions that did the trick. I added 'end' to all my functions (including varargout) and the errors regarding the 'end's went away. Then I used Matt's method and voila. Thanks again. I'm sure I will have more questions soon.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us