Set From Workspace Block from Function

조회 수: 2 (최근 30일)
Alec Reed
Alec Reed 2019년 2월 25일
답변: Rahul Kumar 2019년 4월 27일
I am attempting to set simulink object values from a function to create Parallelizable code. My simple model is a "from Workspace" block to a scope:
I can easily set the simin block to send a rect with the following code:
sim_in = Simulink.SimulationInput('my_model');
simin.time = [0 1 2 3];
simin.signals.values = [0 0 1 0]';
out_data = sim(sim_in)
However I want to scale this up, to sending 100 different rect pulses, I am having trouble setting simin.time and simin.signals.values from within a function.
the goal of the code would look something like this:
mdl = 'my_model';
N = 100;
sim_in(1:N) = Simulink.SimulationInput(mdl);
for i = 1:N
sim_in(i) = sim_in(i).setPreSimFcn(@(x) set_values(x, i));
end
out_data = sim(sim_in);
function set_values(sim_in, t)
simin.time = [0 t t+1 t+2];
simin.signals.values = [0 0 1 0]';
end
How do I set the simin block from within the set_values function? The end goal is to replace the sim(sim_in) with parsim(sim_in).
Thanks!

답변 (1개)

Rahul Kumar
Rahul Kumar 2019년 4월 27일
Hi Alec,
You can use the setVariable method on the SimulationInput object to specify a new value for the variable
Here is your modified script (I have also reduced number of simulations to 10 so that you can pause in the set_values function and see the scope change)
mdl = 'my_model';
N = 10;
sim_in(1:N) = Simulink.SimulationInput(mdl);
for i = 1:N
sim_in(i) = sim_in(i).setPreSimFcn(@(x) set_values(x, i));
end
out_data = sim(sim_in);
function sim_in = set_values(sim_in, t)
simin.time = [0 t t+1 t+2];
simin.signals.values = [0 0 1 0]';
sim_in = sim_in.setVariable('simin', simin);
end
Please try it out and let me know if you have any more questions.
-- Rahul

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by