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 Center および File ExchangeGeneral Applications についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by