Code execution in GUI
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Image Analyst
am 27 Jun. 2018
Are you using GUIDE, or it's replacement "App Designer", to build your GUI?
Akzeptierte Antwort
OCDER
am 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 Kommentare
OCDER
am 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:
Weitere Antworten (1)
Rik
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!