Control a Simulink module by an M-file
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (5개)
Kaustubha Govind
2012년 6월 21일
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
Kaustubha Govind
2012년 6월 27일
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
2012년 6월 27일
Just saw that grapevine gave you a more extensive answer to your comment.
grapevine
2012년 6월 25일
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
grapevine
2012년 6월 27일
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
2012년 7월 18일
댓글 수: 6
Kaustubha Govind
2012년 7월 20일
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
2012년 7월 18일
편집: Albert Yam
2012년 7월 18일
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
Albert Yam
2012년 7월 20일
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
2012년 7월 19일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Schedule Model Components에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!