extract spesific out from simulation to Mfile matlab when run the simulation inside the file
3 views (last 30 days)
Show older comments
mohammed hussein
on 16 Jun 2023
Commented: mohammed hussein
on 16 Jun 2023
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
0 Comments
Accepted Answer
Prannoy
on 16 Jun 2023
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
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!
More Answers (0)
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!