Problem with fast and slow function running after each other

조회 수: 37 (최근 30일)
PrinzM
PrinzM 2024년 10월 29일 14:45
편집: dpb 2024년 10월 30일 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.

채택된 답변

dpb
dpb 2024년 10월 29일 15:57
이동: Voss 2024년 10월 29일 17:31
Instead of pause, try drawnow
  댓글 수: 3
dpb
dpb 2024년 10월 30일 13:35
편집: dpb 2024년 10월 30일 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.
PrinzM
PrinzM 2024년 10월 30일 13:46
Thank you very much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by