How to pass data from .m file to GUI ?
5 views (last 30 days)
Show older comments
Michal Dobrzanski
on 14 Jun 2014
Answered: Image Analyst
on 15 Jun 2014
Hello everyone,
It is my major concern. I would like not to use global variables to this. I have an .m file where have a command:
set(handles.statusbar,'String','foobar');
And then I get the error that the handles are not defined. I know. I just look for the exact function, which would define in my scope (the .m file) the handles to my precious GUI.
I would appreciate your help! Michał
2 Comments
Geoff Hayes
on 14 Jun 2014
Michal - you may need to attach your GUI .m and .fig file which may reveal why the code is behaving in this manner.
Where in the code have you added the statement set(handles.statusbar,'String','foobar');? Is it in a callback where handles is an input? If you don't want to attach your code, then at the very least, please include the complete function (signature and body) that includes this statement.
Accepted Answer
Geoff Hayes
on 14 Jun 2014
Edited: Geoff Hayes
on 14 Jun 2014
I see the concern…you have a callback in the analog output object, add_to_buffer that, when invoked, you want to update the GUI status text widget via the displaytime script (which is also called from the music_Callback function within the GUI. Another global variable could be used, or you could try the following in your displaytime script, just prior to updating the status text widget
% get the handles structure for the current figure/GUI
handles = guihandles;
if ~isempty(handles) && infield(handles,'status')
set(handles.status,'String',[num2str(t1) ':' t2s ' of ' num2str(t10) ':' t20s]);
end
The only problem with the above is that if another GUI obtains focus, then the call to guihandles may return the handles for that object instead, and not your GUI.
You may be able to get around that potential problem as follows - in GUIDE, open the Property Inspector for your figure/gui. When the inspector opens, the title should read Inspector figure(gui). Look for the HandleVisibility property and change it from callback to on. Look at the Tag property, and it should read figure1. Close the inspector and press the save button.
Back in your displaytime script, replace what I showed above with
h = findobj('Tag','figure1');
if ~isempty(h)
handles = guihandles(h);
if ~isempty(handles) && isfield(handles,'status')
set(handles.status,'String',[num2str(t1) ':' t2s ' of ' num2str(t10) ':' t20s]);
end
end
So what the above does is to find the handle the object whose Tag is named figure1 (your GUI). If the returned handle is not empty, then we want to get all handles associated with it by using the guihandles function. If that handles structure is not empty and has a field called status then we set it with the data.
Try this code and see what happens!
EDIT just a note - the HandleVisibility indicator is set to on so that the handle is visible when we call the findobj function.
More Answers (2)
See Also
Categories
Find more on Graphics Object Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!