Can I force Matlab to wait 'til it finishes one command to run another?
Show older comments
I have a script that loads a SimBiology model using the simbiology() command. Then I try to get a handle for the model
simbiology('mymodel.sbproj'); % There are reasons why I use simbiology and not sbioloadproject
sbr = sbioroot;
mdls = sbr.models;
nmdls = numel(mdls);
mc = mdls(nmdls);
The problem is when I first try to get mdls. I get an empty array. Apparently, the model is still loading when the sbr=sbioroot and mdls = sbr.models commands run.
In windows command line files, you can use
START /WAIT myprogram.exe
to force myprogram.exe to finish running before going on the next line. Is there a similar artifice in Matab/Simbiology? Something like
finish('simbiology('mymodel.sbproj')' ;
% or
whilerunning
simbiology('mymodel.sbproj');
end
I keep thinking that there's an easy way that I've forgotten...
The above is a general case. Of course, if simbiology() were re-written to return the model handle, and when the handle was requested, would not allow the script to proceed until the model was loaded, it would be great. Logical, too, because generally when you're loading a model you want to do something with that model before proceeding.
mc = simbiology('mymodel.sbproj'); % This solves the specific problem!
% or
mc = simbiology('mymodel.sbproj','WaitToComplete'); % or this
10 Comments
Guillaume
on 19 Sep 2019
With regards to your question: Can I force Matlab to wait 'til it finishes one command to run another?
This is what matlab and most other languages, do already. A statement will not be executed until the previous one has completed. e.g. the next line of code will not be executed until simbiology returns.
Now your problem is that simbiology itself launches another task in the background and returns more or less immediately. Unfortunately, I don't know anything about simbiology so can't help with that. Typically something that launches a task in the background will have a callback that can be executed when the task is complete.
So the problem, and thus the solution, is not with matlab but with simbiology.
Walter Roberson
on 19 Sep 2019
sbioloadproject()
Jim Bosley
on 19 Sep 2019
Walter Roberson
on 19 Sep 2019
simbiology() itself is the command to start up the simbiology interface and return control to MATLAB. It makes no promises that any project will have finished loading before it returns to MATLAB. For use in a function or script you would sbioloadproject() and sbiosimulate() and related commands.
Jim Bosley
on 19 Sep 2019
Edited: Jim Bosley
on 19 Sep 2019
Walter Roberson
on 19 Sep 2019
simbiology('mymodel.sbproj');
sbioloadproject('mymodel.sbproj');
The simbiology() command might return before the interface is completely open, but the sbioloadproject() after it should not return until the project is completely loaded.
Jim Bosley
on 19 Sep 2019
Edited: Jim Bosley
on 19 Sep 2019
Walter Roberson
on 19 Sep 2019
If you
simbiology('mymodel.sbproj');
then that would start to load the position information, but might not completely finish before it returns. Would the
sbioloadproject('mymodel.sbproj');
discard the position information ?
If so, then you will probably need to open a support request.
Jim Bosley
on 19 Sep 2019
Jim Bosley
on 19 Sep 2019
Edited: Jim Bosley
on 22 May 2020
Accepted Answer
More Answers (0)
Categories
Find more on Build Models in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!