Control a Simulink module by an M-file

5 Ansichten (letzte 30 Tage)
Nelson Silva
Nelson Silva am 19 Jun. 2012
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.

Antworten (5)

Kaustubha Govind
Kaustubha Govind am 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 Kommentare
Kaustubha Govind
Kaustubha Govind am 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
Kaustubha Govind am 27 Jun. 2012
Just saw that grapevine gave you a more extensive answer to your comment.

Melden Sie sich an, um zu kommentieren.


grapevine
grapevine am 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 Kommentare
Nelson Silva
Nelson Silva am 26 Jun. 2012
I connected the clock block to the Fcn block and do nothing else to the function on the Fcn block, staing it like this:
function y = fcn(u)
% This block supports the Embedded MATLAB subset.
% See the help menu for details.
y = u;
Now I want to run the m-file calling the function but I can't, How should I name it in the the m-file?
grapevine
grapevine am 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

Melden Sie sich an, um zu kommentieren.


Nelson Silva
Nelson Silva am 18 Jul. 2012
Thanks for all your answers, but I still can't do what I intend to. Now I tried to change the parameter of the resistance block with a variable in the parameter, and then give a value to this variable in the function block, but I still haven't resolve my problem. Can't anyone send me an example of what I pretende to my email or give any other ideia of how can I do that?
  6 Kommentare
Nelson Silva
Nelson Silva am 20 Jul. 2012
the error is the following
Block error
Error in 'tresist/rc': Parameter 'z' cannot be evaluated. MATLAB error message: Undefined function or variable 'z'.
Kaustubha Govind
Kaustubha Govind am 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')".

Melden Sie sich an, um zu kommentieren.


Albert Yam
Albert Yam am 18 Jul. 2012
Bearbeitet: Albert Yam am 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 Kommentare
Nelson Silva
Nelson Silva am 20 Jul. 2012
The problem is that, i can load 'z' into the model when the simulation is runing. I can only load it when the simulation is stoped by an m-file
Albert Yam
Albert Yam am 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.

Melden Sie sich an, um zu kommentieren.


C.J. Harris
C.J. Harris am 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.
  1 Kommentar
Nelson Silva
Nelson Silva am 19 Jul. 2012
But that's not a resistance value (ohm), that only works as a signal and not what I intend to

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by