Problem with fast and slow function running after each other

37 visualizzazioni (ultimi 30 giorni)
PrinzM
PrinzM il 29 Ott 2024 alle 14:45
Modificato: dpb il 30 Ott 2024 alle 19:56
Dear MathWorks Community,
I am trying my hands at the app designer currently and defined some functions that run after each other for plotting a physics problem. For starters I want to mention that I just use Matlab as a better calculator with little knowledge about code optimization and how it really works.
This is what I currently try to do:
On change of a drop down element do the following:
  1. Lock all editable elements in a graphic container (Tab in my case)
  2. Calculate and Plot a function
  3. Unlock all editable elements in the graphic container
For 1 and 3 I wrote myself a private function, since I use this regulary for "Locking"
function LockTab(app,input)
elements = findobj(input,"Enable","on");
for ii=1:numel(elements)
elements(ii).Enable="off";
end
end
and "Unlocking" a Tab, while calculations are running
function UnlockTab(app,input)
elements = findobj(input,"Enable","off");
for ii=1:numel(elements)
elements(ii).Enable="on";
end
end
now I want to call those functions and the slow plotting function
function MyDropDownValueChanged(app, event)
LockTab(app,app.MyTab)
Plottingfunction(app)
UnlockTab(app,app.MyTab)
end
My expectations now are, that MyTab is "locked", then the PlottingFunction runs, updating my plot, afterwards MyTab is "unlocked".
I measured the computation time of my functions and they take around
0.003seconds
4-6.5 seconds (depending on the DropDown)
0.003 seconds
What happens however is, that MyTab is not locked and just the plotting is done. My solve right now is to add a pause(0.1) line between LockTab and Plottingfunction, but that seems like a bandaid at best and I am puzzled as to why the LockTab function isn't doing anything unless I pause, as the functions should run sequentally and not influence each other to my understanding. It feels as though the PlottingFunction is overwriting the LockTab funciton though.
Thank you in advance for your help.

Risposta accettata

dpb
dpb il 29 Ott 2024 alle 15:57
Spostato: Voss il 29 Ott 2024 alle 17:31
Instead of pause, try drawnow
  3 Commenti
dpb
dpb il 30 Ott 2024 alle 13:35
Modificato: dpb il 30 Ott 2024 alle 19:56
As the doc for drawnow explains, MATLAB may choose to not immediately process graphics or other callback actions that don't affect screen visibility or other events that don't matter from the standpoint of actual end results immediately when the event(s) are posted/queued. If it is desired to force such action as here, drawnow is the documented way in which to do that.
As you've noted, pause may result in the same thing happening as there is then time for the callbacks to be processed, but that is not the reliable way.
The callbacks will get processed, regardless, just not necessarily in the precise order of the instructions as they are queued. That's the thing about interrupt-driven code; it no longer is strictly sequential in execution; queueing delays for short-lived events such as this may end up with the event being over before the queue is cleared unless the system is forced to process any pending events.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by