Set From Workspace Block from Function

2 次查看(过去 30 天)
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