Wait a boolean variable becomes false

36 views (last 30 days)
NF
NF on 26 Apr 2018
Commented: Guillaume on 10 May 2019
I have connected the software Plant Simulation to Matlab and thus when I start the matlab program the simulation on Plant SImulation correctly works. However, after this simulation I have to analyse the data but matlab proceeds even if the simulation is not finished. I am thinking about using the COM-function IsSimulationRunning, which returns true if running and false if not, to say matlab to wait until this variable becomes false.
However I do not know how to do that. This because there should be a dynamic control of this variable to see if it is true or not.
How can I do that? Thanks

Accepted Answer

Guillaume
Guillaume on 26 Apr 2018
Well, you can always write a while loop that test that the property is false.
%start simulation
while yourcomobject.IsSumlationRunning
%waiting for simulation to complete
end
%simulation is no longer running
%rest of the code.
However, if your com object triggers an event when the simulation completes, you may want to listen to that event instead. Details of the event would be required to explain how to do that.
  28 Comments
NF
NF on 3 May 2018
Ok, it works with the for cycle now. Thanks!!

Sign in to comment.

More Answers (1)

Alec Jacobson
Alec Jacobson on 10 May 2019
The accepted answer will cause your CPU to spin (wasting precious power and computational resources).
Instead, I found a "hack" that uses a built in function. Create a phony graphics element (e.g., with plot), tell matlab to waitfor it to be deleted and then delete it when you're ready to break out of the loop.
For example, this part of the code will wait for dsh to be deleted.
hold on;
dsh = plot(nan,nan);
hold off;
waitfor(dsh)
In your other code you can delete the dsh object to break out of the waitfor:
delete(dsh)
  1 Comment
Guillaume
Guillaume on 10 May 2019
The accepted answer will cause your CPU to spin (wasting precious power and computational resources).
Notwithstanding the fact that your solution has nothing to do with COM and so is probably not applicable, if you read the whole discussion you'll see that I've extensively explained how to handle COM events asynchronously, which would not waste anything or wait for anything. However, the OP ended up wanting a synchronous handling.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!