From: Adam on
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
From: Nick on
I think you can just add a field to handles and pass that around. So if you put anywhere handles.value=5, since handles gets passed all over the place you can access that value in any of the GUI functions.

-OR-

I started with GUIDE on my project but then switched to creating the figure and callbacks myself this way...

function yourproject

%any code you want to do first

%then create the GUI

fh=figure('position',figpos);
%put a slider in it (or whatever uicontrol you want)
slider1=uicontrol(fh,'style','slider','tag','slider1',...
'units','characters','position',slipos,...
'callback',@slider1_callback,...
'min',1,'max',10,...
'sliderstep',1,'value',1);
%notice the callback is defined as @slider1_callback
%add more uicontrols

%now, do NOT end your code
function slider1_callback(hObject,evendata,handles)

%code to do with slider function
slidervalue=get(hObject,'Value');
%use "get" and "set" to manipulate GUI

end %this ends the slider callback

%more code to do with other GUI objects, put an "end" on each one

%after all your callback functions end your main function
end %end yourproject

I think this will allow you to have global variables throughout
From: Steven Lord on

"Adam " <abc5(a)ubc.ca> wrote in message
news:htotfe$p06$1(a)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(a)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


From: Walter Roberson on
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?
From: Adam on
"Nick " <notavail(a)gmail.com> wrote in message <htousq$nuc$1(a)fred.mathworks.com>...
> I think you can just add a field to handles and pass that around. So if you put anywhere handles.value=5, since handles gets passed all over the place you can access that value in any of the GUI functions.
>
> -OR-
>
> I started with GUIDE on my project but then switched to creating the figure and callbacks myself this way...
>
> function yourproject
>
> %any code you want to do first
>
> %then create the GUI
>
> fh=figure('position',figpos);
> %put a slider in it (or whatever uicontrol you want)
> slider1=uicontrol(fh,'style','slider','tag','slider1',...
> 'units','characters','position',slipos,...
> 'callback',@slider1_callback,...
> 'min',1,'max',10,...
> 'sliderstep',1,'value',1);
> %notice the callback is defined as @slider1_callback
> %add more uicontrols
>
> %now, do NOT end your code
> function slider1_callback(hObject,evendata,handles)
>
> %code to do with slider function
> slidervalue=get(hObject,'Value');
> %use "get" and "set" to manipulate GUI
>
> end %this ends the slider callback
>
> %more code to do with other GUI objects, put an "end" on each one
>
> %after all your callback functions end your main function
> end %end yourproject
>
> I think this will allow you to have global variables throughout

Thanks. I actually started with manual programming too; I thought GUIDE might be restrictive. My program works with manual programming, but it is getting quite complex. Now, I don't know which route to take.

It is a GUI for some numerical algorithms. I basically have a input panel with a bunch of edit boxes, radio buttons, some buttons for importing/exporting files and a start button, to start my algorithm. Later, I will be adding algorithms to the GUI and letting the user choose which to use based on toggle buttons. This will change which parameters are the input panel. The output panel is similar in function but has a bunch of plots and results. Do you recommend programming each object or using GUIDE? It seems like layout will be a problem with programming, but I may run into problems dynamically updating the GUI in GUIDE.