Control a Simulink module by an M-file
2 views (last 30 days)
Show older comments
Hi, I need to change values in time in a simulink simulation, I tried first to do a simple example wich have a source and a resistance, I tried to change the value of the resistance with the following m-file:
z=100
set_param('test', 'SimulationCommand', 'start')
if tout > 3
z=10
end
I saw in other blogs that the "tout" is considered the time of the simulation. The simulation runs, but the resistance only takes the value of 100.
Can you please help me? It's probably something very simple I guess.
0 Comments
Answers (5)
Kaustubha Govind
on 21 Jun 2012
What you're doing with the command "set_param('test', 'SimulationCommand', 'start')" is the equivalent of hitting the "Play" button the model. The model simply logs "tout" to the MATLAB workspace as an array (not as a scalar). Also, there is no guarantee that tout is updated immediately during simulation - it is only guaranteed that at the end of simulation "tout" is a vector containing the list of all time-steps that the model ran at. Even if "tout" were a scalar and were updated instantly, the line "if tout>3" would have to run at the exact instant that tout became >=3. You probably need to put in some kind of while loop to be more realistic, but like I said, even a while loop will not help in this case.
What you might need to do instead is write something like a MATLAB Function that reads the simulation time value (perhaps from the Clock block) and performs the set_param. You can call this function from one of three MATLAB Function blocks so it is run at every simulation time-step. Alternatively, you could use a solution similar to this FEX submission.
5 Comments
Kaustubha Govind
on 27 Jun 2012
Ah! You are using the MATLAB Function block, not the Fcn block. In any case, you should be able to call your function from that block. Just edit the function to do something like:
function y = fcn(u)
% This block supports the Embedded MATLAB subset.
% See the help menu for details.
y = callmyfunction(u);
Kaustubha Govind
on 27 Jun 2012
Just saw that grapevine gave you a more extensive answer to your comment.
grapevine
on 25 Jun 2012
You might use a clock block
connected to a Matlab Fcn block,
which is associated to a m-file, in my example I called it stopSimulation.m. Within this mfile I wrote this code
function [isTChanged] = stopSimulation(u)
if(u>3)
set_param(gcs,'StopTime','4')
isTChanged=1
else
isTChanged=0
end
which is capable to change the simulation duration to 4 s after the first 3s
good luck
2 Comments
grapevine
on 27 Jun 2012
you should replace the default code of the Matlab function block
"
function y = fcn(u)
% This block supports the Embedded MATLAB subset.
% See the help menu for details.
y = u;
"
with this one :
"
function [isTChanged] = stopSimulation(u)
if(u>3)
set_param(gcs,'StopTime','4')
isTChanged=1
else
isTChanged=0
end
"
that's all
for further details take a look at this doc
http://www.mathworks.nl/help/toolbox/simulink/ug/f6-106261.html
Nelson Silva
on 18 Jul 2012
6 Comments
Kaustubha Govind
on 20 Jul 2012
If using a variable called 'rr' to represent the value of the parameter, you need to define 'rr' with a numerical value in the workspace and say "set_param('tresist/rc', 'z', 'rr')".
Albert Yam
on 18 Jul 2012
Edited: Albert Yam
on 18 Jul 2012
So you want to run the simulation, and have 'z' change from 100 to 10 at 3 seconds? Is 'z' a constant block? Change it to a Lookup-table (n-D), 1 table dimension
ts = 0.1; %timestep?
z.time = [0 3-ts 3 t_final]; %breakpoint set
z.signals.values = [100 100 10 10]; %table data
set_param('test', 'SimulationCommand', 'start')
Edit: You need a clock block into the Lookup-table.
4 Comments
Albert Yam
on 20 Jul 2012
You still haven't answered the question, about what block you are using to load 'z'. Are you using a [resistor] block? I am guessing that works like a [constant block]. A [constant block] will NOT update while the simulation is running, so you have to use a different block. Use a [Variable Resistor], with an input. I suggested a lookup table, so that you can define beforehand,
from 0-3, set z = 100
from 3-t_final set z = 10
C.J.Harris's 'step' block also does this, and works in this manner.
C.J. Harris
on 19 Jul 2012
You are all making this more complicated than it actually is. Use the 'Step' block. Set the initial value to '100', set the final value to '10', and then set the step-time to 3.
See Also
Categories
Find more on Schedule Model Components 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!