Problem with fast and slow function running after each other

4 views (last 30 days)
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.

Accepted Answer

dpb
dpb on 29 Oct 2024
Moved: Voss on 29 Oct 2024
Instead of pause, try drawnow
  3 Comments
dpb
dpb on 30 Oct 2024
Edited: dpb on 30 Oct 2024
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!