extract spesific out from simulation to Mfile matlab when run the simulation inside the file

1 次查看(过去 30 天)
Dear all
I have a question about running the simulation inside the m file, extracting specific data, and using it in the m file. for example
I have simulink in the name (Mysimulink) works to get the error (eT) . I want to run the Simulink inside the loop and each loop extracts the error (eT) and uses it in another calculation. please note I used eT as a block (to workspace)
for i=1:10
e=3+i
sim('Mysimulink.slx'); % i have eT in block the (to workspace)
error=eT +e
end
when I run the mfile it gives me an error (tout ) not specified and some other errors
Thank you for your helping

采纳的回答

Prannoy
Prannoy 2023-6-16
When you run a simulation using sim() command in MATLAB, you need to specify the simulation time by providing a time vector tspan or a simulation time tf. By default, the time vector is set to [0, 10] when you create a new empty Simulink model, and this time is used by default when you run the simulation using the "Start" button in Simulink.
However, when you run the simulation using sim() inside a loop, you also need to specify the simulation time for each iteration of the loop. One way to do this is to specify the same tspan for each iteration, as follows:
tspan = [0, 10]; % simulation time for each iteration of the loop
for i = 1:10
e = 3 + i;
simout = sim('Mysimulink', 'StartTime', num2str(tspan(1)), 'StopTime', num2str(tspan(2))); % run the simulink model and save the sim output
eT = simout.get('eT'); % extract the error (eT) from the sim output
error = eT + e; % use the error (eT) in another calculation
end
Unable to find system or file 'Mysimulink'.
Here, we set tspan to [0, 10] to match the default simulation time in Simulink. Then inside the loop, we use the sim() command to run the Simulink model, and set the 'StartTime' and 'StopTime' options to num2str(tspan(1)) and num2str(tspan(2)), respectively, to specify the simulation time. We also save the output from the Simulink model using the simout variable.
To extract the error (eT) from the Simulink output, you can use the get() method of the simout variable, as shown above. Finally, you can use the error (eT) in another calculation, as desired.
Hope this helps!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by