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 Functions에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by