How to pass data from .m file to GUI ?

5 views (last 30 days)
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
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.
Michal Dobrzanski
Michal Dobrzanski on 14 Jun 2014
The statement set(handles.statusbar,'String','foobar') is in the displaytime script. This script is firstly in the scope, but then is continuously opened in 'SamplesOutputFcn' for the audioplayer. I have an audioplayer, which should update the time count.

Sign in to comment.

Accepted Answer

Geoff Hayes
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.
  2 Comments
Michal Dobrzanski
Michal Dobrzanski on 15 Jun 2014
It did a great job. Thank you! The second option was effective.

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 14 Jun 2014
Use guidata function
  1 Comment
Michal Dobrzanski
Michal Dobrzanski on 14 Jun 2014
This doesn't help me. When I type before the 'set' command to get the info:
data = guidata(handles.statusbar);
The error comes out..

Sign in to comment.


Image Analyst
Image Analyst on 15 Jun 2014

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!