Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Control a Simulink simulation via m-file or command line

Subject: Control a Simulink simulation via m-file or command line

From: Simon Martin

Date: 29 Aug, 2008 08:11:31

Message: 1 of 7

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

Subject: Control a Simulink simulation via m-file or command line

From: Phil Goddard

Date: 29 Aug, 2008 16:33:01

Message: 2 of 7


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.

Subject: Control a Simulink simulation via m-file or command line

From: Simon Martin

Date: 1 Sep, 2008 07:19:38

Message: 3 of 7

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

Subject: Control a Simulink simulation via m-file or command line

From: Mathieu Claeys

Date: 1 Sep, 2008 08:50:18

Message: 4 of 7

"Phil Goddard" <philgoddardNOSPAM@telus.net> wrote in
message <g998bt$agp$1@fred.mathworks.com>...
>
> 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.
>

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

Subject: Control a Simulink simulation via m-file or command line

From: philgod23@gmail.com

Date: 1 Sep, 2008 14:18:51

Message: 5 of 7


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.

Subject: Control a Simulink simulation via m-file or command line

From: Mathieu Claeys

Date: 2 Sep, 2008 06:15:04

Message: 6 of 7

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

Subject: Control a Simulink simulation via m-file or command line

From: khalid Al-Abri

Date: 9 Aug, 2010 09:34:07

Message: 7 of 7

"Phil Goddard" <philgoddardNOSPAM@telus.net> wrote in message <g998bt$agp$1@fred.mathworks.com>...
>
> 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.
>

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us