Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Control a Simulink simulation via m-file or command line

510 views
Skip to first unread message

Simon Martin

unread,
Aug 29, 2008, 4:11:31 AM8/29/08
to
Hi,

I would like to control my Simulink simulation with a simple m-file.

I'd like to start the simulation and after some time i would like to
change the inputs to the model and continue the simulation. Below is
an m-file, but the problem is here that the simulation produces
arbitrary results (at least not the same on every run).

As far as I see the sim() function is not applicable to my problem as
the simulation can't be suspended after some time.


Any hints?

Thanks,
Simon

% simulate.m
% Set input
in = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];

% Start simulation
set_param('Model','SimulationCommand','Start')

% Pause simulation, if time is > 100
% ATTENTION: Works only, if "Limit data points to last" is set to 1 in
Model Explorer!
% Otherwise tout is a vector
if tout > 100
set_param('Model','SimulationCommand','Pause')

% modify inputs
in = [0 1 0 1 0 1 1 1 1 0 1 1 1 1 1];

%Continue simulation
set_param('Model','SimulationCommand','Continue')
end

if tout > 1000
set_param('Model','SimulationCommand','Stop')
end

Phil Goddard

unread,
Aug 29, 2008, 12:33:01 PM8/29/08
to

Note that Simulink does not write tout (or any other data)
to the MATLAB workspace until the simulation is paused or
stopped.
(I'm assuming here that tout is the standard time variable
created on the Data Input/Output panel of the model's
Configuration Parameters.)

Hence if you do not have a variable called tout ALREADY in
your workspace then the code you give would never stop the
model, it would run until the stop time specified in the
model.

There are several ways to achieve what you want.
One would be to have the model accept intial (state)
conditions from the workspace.
That can be specified on the Data Input/Output page.

Then you can use sim
x0 = ...... % whatever you need
in = ...... % whatever you want
[t,xVec,y] = sim('model',[startTime pauseTime]);

x0 = xVec(end,:); % ensure the next sim starts where the
first one stopped
in = ...... % the new values
[t,xVec,y] = sim('model',[pauseTime stopTime]);

Note that with the above code the simulation is being run
twice, with each run being over a different time period.
It is not being run once with a pause.

Phil.

Simon Martin

unread,
Sep 1, 2008, 3:19:38 AM9/1/08
to
Hi,

thanks for your reply.

> Note thatSimulinkdoes not write tout (or any other data)


> to the MATLAB workspace until the simulation is paused or
> stopped.

Ah okay, that explains a lot.


> Hence if you do not have a variable called tout ALREADY in
> your workspace then the code you give would never stop the
> model, it would run until the stop time specified in the
> model.
>
> There are several ways to achieve what you want.
> One would be to have the model accept intial (state)
> conditions from the workspace.
> That can be specified on the Data Input/Output page.
>
> Then you can use sim
> x0 = ...... % whatever you need
> in = ...... % whatever you want
> [t,xVec,y] = sim('model',[startTime pauseTime]);
>
> x0 = xVec(end,:); % ensure the next sim starts where the
> first one stopped
> in = ...... % the new values
> [t,xVec,y] = sim('model',[pauseTime stopTime]);
>
> Note that with the above code the simulation is being run
> twice, with each run being over a different time period.
> It is not being run once with a pause.

I tried something like that, but then the behavior is a little
strange.

Is there really no possibility to pause a simulation based on a time
condition?


Simon

Mathieu Claeys

unread,
Sep 1, 2008, 4:50:18 AM9/1/08
to
"Phil Goddard" <philgodd...@telus.net> wrote in
message <g998bt$agp$1...@fred.mathworks.com>...

Just to add two little comments if you use Phil's method.
First, beware that some non-linear blocks will not have
their internal states saved in the Xo vector. The most
common one is the zero-order hold (will be zero at the
next sim() start-up). Second, it is safer to use structure
formats with state I/O than using vectors, because the
state order can be swapped without you noticing it (even
if you run the simulations in a row). You can achieve this
using the SaveFormat option in simset.

Mathieu

phil...@gmail.com

unread,
Sep 1, 2008, 10:18:51 AM9/1/08
to

If you're willing to change your model (i.e. add some blocks), then
you can use an Assertion block.
For an example look in the Simulink doc under
Running Simulations -> Controlling Execution of a Simulation

Phil.

Mathieu Claeys

unread,
Sep 2, 2008, 2:15:04 AM9/2/08
to
Simon Martin wrote

> Is there really no possibility to pause a simulation
based on a time
> condition?

No, I don't think there is unless you change your model by
adding blocks like S-functions to control the simulation
flow (look on the past posts for something like simulink
gui control, there are plenty of examples).

Regards,

Mathieu

khalid Al-Abri

unread,
Aug 9, 2010, 5:34:07 AM8/9/10
to
"Phil Goddard" <philgodd...@telus.net> wrote in message <g998bt$agp$1...@fred.mathworks.com>...
>

Hi Phil.

Can you please explain the above code , I mean what are mean t,y,x0 and xVec
because I need this code in my project

I hope to replay soon
and Thank you in advance

0 new messages