Code execution in GUI

5 visualizaciones (últimos 30 días)
Vittorio
Vittorio el 27 de Jun. de 2018
Comentada: OCDER el 28 de Jun. de 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 comentarios
Image Analyst
Image Analyst el 27 de Jun. de 2018
Are you using GUIDE, or it's replacement "App Designer", to build your GUI?
Vittorio
Vittorio el 28 de Jun. de 2018
I have used GUIDE but if the App designer helps me solve my problem I can learn how to use it.

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 27 de Jun. de 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 comentarios
Vittorio
Vittorio el 28 de Jun. de 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 el 28 de Jun. de 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:

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 27 de Jun. de 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

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by