Using Simulink from .m file
조회 수: 51 (최근 30일)
이전 댓글 표시
I have my whole code written in a gigantic .m file. In a certain process I am doing in my code, I want to call this block (shown in picture) and give it "F" and "Ma" and take its outputs back from simulink to my code's workspace. I am struggling to do this correctly and the sim command page is poorly written to help me with this. Any help is appreciated.
댓글 수: 6
Paul
2023년 1월 16일
편집: Paul
2023년 1월 16일
I'm not asking about the values, I'm asking about the blocks themselves. Can you double click on each block and post a screenshot of the block parameters window that pops up for each? Image files can be uploaded using either the paperclip icon or the image icon (to the left of link icon) in the Insert menu.
채택된 답변
Paul
2023년 1월 17일
Ok. If F and Ma are constants, you should consider using a Constant block for each. Make the "Constant value" paramter F and Ma, just like you already have, in each block respectively. That would probably make things a bit easier.
Assuming we are going to stick with the From Workspace block and you want F and Ma to both be constant over the simulation, the variables F and Ma actually need to be defined as F = [0 Fx Fy Fz] and Ma = [0 Max May Maz], which means set the ouput of the blocks to [Fx Fy Fz] and [Max May Maz] at time = 0. The outputs will be held constant for time > 0.
% initialize Fx Fy Fz and Max May Maz to whatever values for the first
% run
Max = 1; May = 1; Maz = 1;
Fx = 1; Fy = 1; Fz = 1;
% Initialize the Simulink.SimulationInput for the model
simIn = Simulink.SimulationInput('mysim'); % use whatever your model name is
for nrun = 1 : 5 % five runs, or whatever
% set F and Ma in simIn
simIn = setVariable(simIn, "F",[0 Fx Fy Fz],"Workspace",'mysim');
simIn = setVariable(simIn, "Ma",[0 Max May Maz],"Workspace",'mysim');
% run the simulation
simOut = sim(simIn, 'ShowSimulationManager', 'on'); % or turn ShowSimulationManger off if desired
% compute the updated values of Fax, etc. based on simOut for use next
% time through the loop
Fx = % etc
end
댓글 수: 2
Paul
2023년 1월 17일
You can add the line:
simIn = setModelParameter(simIn,'StopTime',1);
to only run the simulation for 1 second. If you're using a Fixed Step solver, just set the stop time to the solver time step.
Would you mind describing what you're doing? Maybe you should be computing the updates to F and Ma in the simulation itself.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!