Update GUI from external function

There have been multiple topics on this question but I believe my situation is a bit different. My GUI triggers a simulation, which takes quite a while to finish (up to 15 minutes depending on propagation time).
Say my GUI contains the following callback:
function generate_button_Callback(hObject, eventdata, handles)
clc
[Total_Orbits] = Main_simulation(....)
Inside the function Main_simulation there is a remaining time estimation. Currently using disp() in Main_simulation to have it show in the command window. However, I would like to have this update in a static text box in my GUI. However, the GUI.m function is "frozen" until the entire Main_simulation() has run.
I am thus looking for a sort of solution to force/push a change change to the GUI interface without having to wait until the entire Main_simulation() function has finished. Is this even possible?
Thank you!

답변 (1개)

Adam
Adam 2017년 4월 26일

1 개 추천

This setup should work and is the kind of thing I use:
In a totally separate file, on your path, define this class:
classdef TimeEstimate < handle
properties( SetObservable )
time = 0;
end
end
Then back in your code you show above, add this:
timeEst = TimeEstimate;
timeListener = addlistener( timeEst, 'time', 'PostSet', @(src,evt) updateProgressInGUI( hObject, timeEst ) );
[Total_Orbits] = Main_simulation(....,timeEst );
and somewhere else in your GUI file add this function definition, adding whatever you need to put the time on the GUI.
function updateProgressInGUI( hObject, timeEstimate )
handles = guidata( hObject )
% Do whatever you want here to update your GUI components based on timeEstimate.time
There are other options too and you may be able to use the usual src argument to the callback rather than passing in the timeEstimate object, I can't remember off the top of my head what the first callback argument represents for a PostSet listener so this is the approach I use.
Alternatively you can inject a callback and handling of it directly into the TimeEstimate class which reacts when 'time' is set - sometimes I do this also, but it amounts to the same thing so I chose the listener approach here.

댓글 수: 2

Sander Voss
Sander Voss 2017년 4월 26일
Hi Adam thank you for your answer!
I'm currently not at my computer but I'll try this tonight.
Just to be clear that separate file can have any name and does not have to be declared anywhere as long as it is in the same folder?
It must have the same name as the class (which, of course, you can call whatever you like). It just needs to be on your Matlab path somewhere.
The one bit I missed is that of course you need to update that 'time' property within your Main_simulation program. This can be done simply as
timeEst.time = ...
from wherever your calculation is.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2017년 4월 26일

댓글:

2017년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by