I am new to MATLAB GUI and would really appreciate some help for a project I am doing.
I am currently designing a MATLAB GUI where a .mat file is loaded using uigetfile or uiopen'load' and upon hittng a callback button, it will plot a graph based on the values in the .mat file.
However, I am facing some problems.
When I want to use a button callback to plot graphs using the values in the .mat file, I am unable to do so as the callback function cannot read values outside of its function itself, therefore giving an error (undefined variables)
I am confused whether it is more appropriate to use handles or guidata outside the function to enable the callback function to read values to plot graphs.
Any help to get me started will be greatly appreciated! Eg. example of how to put the data from .mat file into handles.
Thank you!
When you are working with Matlab GUI , if you want to use variables you have to store data in the figure to use those in another functions.
for an example,
%%
function main()
% all stuff you create in main function
% need to make some variable called 'segmentgui' for the main figure
setappdata(0,seggui,gcf)
uicontrol('style','push','callback',@ mypushcallback);
end
function mypushcallback(varargin)
[filename, pathname] = uigetfile({'*.mat', 'Text Files (*.mat)'});
data1 = [pathname filename]; % now i get the complete path name
segmentgui = getappdata(0, 'segmentgui'); % calling the function variable.
setappdata(segmentgui, 'filename', data1) % storing the data1 to segmentgui struc.
end
function calculation
segmentgui=getappdata(0,'segment')
filename=getappdata(segmentgui,'filename');
% now i call the segmentgui variable and calling back my data filename in it.
% now you can use the filename.
yourvalues = load filename
plot( yourvalues)
end
function calculation2
segmentgui=getappdata(0,'segment')
filename=getappdata(segmentgui,'filename');
% so on
end
-----------------------------------
try help commands
rmappdata, setappdata, getappdata.
you can better understand.
Regards,
Raady !!
"Louis " <lo...@ezcommercehost.com> wrote in message
news:imhqcd$n41$1...@fred.mathworks.com...
> Hello everyone,
>
> I am new to MATLAB GUI and would really appreciate some help for a project
> I am doing.
>
> I am currently designing a MATLAB GUI where a .mat file is loaded using
> uigetfile or uiopen'load' and upon hittng a callback button, it will plot
> a graph based on the values in the .mat file.
>
> However, I am facing some problems.
>
> When I want to use a button callback to plot graphs using the values in
> the .mat file, I am unable to do so as the callback function cannot read
> values outside of its function itself, therefore giving an error
> (undefined variables)
That is correct. The callback, unless it's a nested function, operates in
its own workspace and cannot directly access variables in other functions'
workspaces.
> I am confused whether it is more appropriate to use handles or guidata
> outside the function to enable the callback function to read values to
> plot graphs.
If you're creating the GUI using GUIDE, refer to this section of the
documentation for a description of some of the techniques you can use to
share data among your callback functions.
http://www.mathworks.com/help/techdoc/creating_guis/f5-998197.html
If you're creating your GUI programmatically, the appropriate section of the
documentation is:
http://www.mathworks.com/help/techdoc/creating_guis/f13-998197.html
--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com