Code execution in GUI

13 views (last 30 days)
Vittorio
Vittorio on 27 Jun 2018
Commented: OCDER on 28 Jun 2018
I am trying to build my first GUI. I have what I think is a very dumb question. The GUI has a button that, when pressed, asks the user to select a file (using uigetfile). The program reads data from the file and updates a popup menu with information contained in the selected file. I have done this without problem inside the button callback function and it works.
After this, the program is essentially supposed to enter an infinite loop, where it keeps reading data from the selected file and updating a plot with them. The data used for the plot change based on the user selection in the popup menu. Therefore I need to both keep plotting and be able to catch the popup selection change.
I simply don't know where to put the plotting code. I can't put it in the button callback function, otherwise I won't be able to process the popup callback. For the same reason I can't put it in the popup callback (I'd catch the first selection change in the popup but none after that). So, where am I supposed to put the plotting code?
Thanks
  2 Comments
Image Analyst
Image Analyst on 27 Jun 2018
Are you using GUIDE, or it's replacement "App Designer", to build your GUI?
Vittorio
Vittorio on 28 Jun 2018
I have used GUIDE but if the App designer helps me solve my problem I can learn how to use it.

Sign in to comment.

Accepted Answer

OCDER
OCDER on 27 Jun 2018
You could use a timer object to run the plots in the background until it receives a stop signal. See the example code and fig as a starting point. It'll draw something forever and changing something in the listbox will alter the plots in real time.
  3 Comments
Vittorio
Vittorio on 28 Jun 2018
I have to look at your code carefully but I think it does exactly what I need. Time to learn something new! Thanks!
OCDER
OCDER on 28 Jun 2018
You're welcome! Note that this is still a workaround solution as matlab GUI doesn't work well with situations that should use multiple threads - one thread for plotting and one thread for detecting changes to your GUI. Maybe it changed in the newer versions. Read this:

Sign in to comment.

More Answers (1)

Rik
Rik on 27 Jun 2018
What you could do is something like the following:
f=figure(1);clf(1)
handles=struct('f',f,'interuptor',true,'counter',0);
guidata(f,handles)
uicontrol(...
'Parent',f,...
'Units','normalized',...
'Position',[0.25 0.25 .5 .5],...
'String','Click me',...
'callback',@buttoncallback)
function buttoncallback(~,~)
handles=guidata(gcbf);
%set an interuptor
handles.interuptor=true;handles.allowWindowClose=false;
guidata(gcbf,handles)
%get the data with uigetfile etc (you might need a uiwait in here)
uiwait(msgbox('foo'))
%run the looper
handles.interuptor=false;
guidata(gcbf,handles)
try
while true
%reload the handles to see if the interuptor was set
drawnow,handles=guidata(h_fig);
if handles.interuptor
break
end
looper_function(handles)
end
catch ME
if strcmp(ME.identifier,'MATLAB:guidata:InvalidInput')
%figure was closed
else
rethrow(ME)
end
end
end
function looper_function(handles)
%put your loop code here
%(example code:)
button=get(handles.f,'Children');
button.String=datestr(now);
end

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!