Run Simulink Model iteratively from .m code

6 次查看(过去 30 天)
Hello
Within a Matlab script I want to run a (not too complicated) Simulink model in many iterations such that the Simulink model continues from the last state, like:
  • in .m file do calculations for a new timestep,
  • based on that: run Simulink, store all model states
  • in .m file, do next calculations
  • contine Simulink model from the previous state
This loop would run e.g. 1000x. I tried to do it the other way around and embedded the .m code within Simulink. But the Compiler did not work (many different dynamic classes in the .m code)

回答(1 个)

Sulaymon Eshkabilov
This exercise of yours can be done in a few different ways.
(1) Store time steps (time array) along with the input data in a cell array, e.g. Data or initially specify time step size if known
(2) Employ sim() to run your Simulink model from MATLAB environment.
(3) In Simulink model, use [From WorkSpace] and [To WorkSpace] blocks, which are used to get the data from MATLAB workspace to simulate the model and send the results back to MATLAB workspace.
(4) In MATLAB code, use sim() to run the SImulink model within a loop. E.g.:
Data = ...
for ii = 1:1000
IN = Data{ii}; % Call [IN] for simulation using [From WorkSpace] block variable's name is IN
OUT{ii}= sim('M_Run.slx'); % Simulation results from the Simulink model M_Run.slx. All simulation results saved under the var name OUT
% Note OUT can be also save a a structure or an array as well)
end

类别

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

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by