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
Paul 2023년 1월 16일
Hi Ali,
Are the blocks for F and Ma both Constant blocks? If so, what is their respective "Constant value" fields? If not, what are those blocks?
Ali Almakhmari
Ali Almakhmari 2023년 1월 16일
편집: Ali Almakhmari 2023년 1월 16일
Well, let me explain it further. I have a gigantic .m file with a gigantic code that fits inside a for loop. For each iteration in the for loop, I step further in time by a pre-designated number of seconds. And in each step of time, F and Ma change, and for each change in F and Ma, I need to compute the values that come out of the box shown in the original picture for them because those values will help me obtain the new F and Ma, and so on. I just keep doing this:
1- Step inside the for loop
2- take the initial F and Ma to compute the outputted quantities from the Simulink box
3- Find new F and Ma from the quantities
4- Keep repeating step 2 and step 3...and so on until a certain condition is falisified or the iterations finish
Paul
Paul 2023년 1월 16일
Yes, I understand what your're doing (I assume you have a good reason for doing step 3 in the .m file instead of in the simulation itself). To help you solve the problem, I'd need to know:
Are the blocks for F and Ma both Constant blocks? If so, what is their respective "Constant value" fields? If not, what are those blocks?
Ali Almakhmari
Ali Almakhmari 2023년 1월 16일
In a single for loop iteration, the values for F and Ma are constant, yes. They can be anything. Example: F = [1,2,3], Ma = [0.1,0.2,0.3];
Paul
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.
Ali Almakhmari
Ali Almakhmari 2023년 1월 17일
Those are just the usual "To Workspace" and "From Workspace" blocks. I took a screenshot of the F variable block. I hope it helps.

댓글을 달려면 로그인하십시오.

 채택된 답변

Paul
Paul 2023년 1월 17일

1 개 추천

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.
So the workflow would look like this, see this doc page for more details. Modify as needed.
% 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

Ali Almakhmari
Ali Almakhmari 2023년 1월 17일
편집: Ali Almakhmari 2023년 1월 17일
Thank you so much. Do you know if I can force Simulink to run for one time step only instead of reporting back multiple time steps?
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개)

카테고리

도움말 센터File Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2023년 1월 16일

댓글:

2023년 1월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by