Borrar filtros
Borrar filtros

Wait a boolean variable becomes false

18 visualizaciones (últimos 30 días)
NF
NF el 26 de Abr. de 2018
Comentada: Guillaume el 10 de Mayo de 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

Respuesta aceptada

Guillaume
Guillaume el 26 de Abr. de 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 comentarios
Guillaume
Guillaume el 2 de Mayo de 2018
There are several problems there:
  • registerevent should only be called once. What it does is tell the object PlantSimulation "Each time you trigger the event SimulationFinished, call my function SimulationFinishedHandler". It should be called before you start the first simulation otherwise you may miss the first event. The best place to register the event would be just after you created the object.
  • As I've said before this is all working asynchronously. The calling of SimulationFinished is completely independent of your main code. It happens whenever the COM object says it is finished regardless of what your main code is doing. Your main code may be even be finished. As a corrolary, your main code runs indenpently of SimulationFinishedHandler, it does not wait for it to execute.
Therefore, with your loop what happens is that you reset the simulation start the simulation, possibly too late register the event, then immediately move on to the next step of the loop independently of whether or not the simulation has finished, reset the simulation (which may still be running) and start a new simulation. Then needlessly re register the event.
If you want synchronous code, then we're back to my very first answer
PlantSimulation = actxserver('Tecnomatix.PlantSimulation.RemoteControl.13.0');
PlantSimulation.LoadModel('C:\Users\***');
for ii=1:2
PlantSimulation.ResetSimulation('.Models.Frame.EventController');
PlantSimulation.StartSimulation('.Models.Frame.EventController');
while PlantSimulation.IsRunning %wait for simulation to complete
end
%simulation is now finished
%do some processing
end %move on to the next simulation.
NF
NF el 3 de Mayo de 2018
Ok, it works with the for cycle now. Thanks!!

Iniciar sesión para comentar.

Más respuestas (1)

Alec Jacobson
Alec Jacobson el 10 de Mayo de 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 comentario
Guillaume
Guillaume el 10 de Mayo de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by