MATLAB Answers

Importing a SIMULINK model to workspace and using the model for output generation

5 views (last 30 days)
Recep Araz
Recep Araz on 14 Jan 2020
Commented: Recep Araz on 29 Jan 2020
Hey there,
I created a model in SIMULINK and I have 16 signals that I want to feed in to this system and obtain the outputs individually with coding in MATLAB.
I can do this one by one using the From Workspace and To Workspace Blocks in SIMULINK, however I want to do this many times using a single for loop in MATLAB.
How can I load the system to MATLAB workspace, feed my signals one by one and store the output?
Thanks a lot !

  0 Comments

Sign in to comment.

Answers (1)

Samatha Aleti
Samatha Aleti on 27 Jan 2020
Edited: Samatha Aleti on 28 Jan 2020
Hi,
According to my understanding, you are trying to run a SIMULINK model with different values each time one after other, from MATLAB. You can do this with the help of sim” command and “set_param” command. Here is a sample MATLAB script to do this:
y = zeros(1,10); % Let
A = 0; % Let
for i = 1:10
A = A +1;
set_param('SimLoop/Constant','Value','A') % Changing the constant value for each simulation
out = sim('SimLoop'); % Run model
y(i) = out.yout.Data; % Save output
end
I have attached the SIMULINK model SimLoopthat I used in this example for better understanding. This is basic example to help you understand how to use commands to load and configure a SIMULINK model, you may have to add or modify to get your desired behaviour.
You can also use the command “parsim” to run multiple simulations in parallel, Refer the following document link for detailed explanation:
Following is the document link on Programmatic Modeling:

  1 Comment

Recep Araz
Recep Araz on 29 Jan 2020
Thank you so much for your help! I learned about the set_param method and I tried to use it.
In your example however, this error occurs
Invalid Simulink object name: SimLoop/Constant
Caused by:
Error using Untitled (line 5)
No block called 'Constant' could be found.
In my case, I am trying to get and set the PID parameters(Kp,Ki,Kd,N) of the PID Controller Block inside a subsystem in my model but I think it's not possible to set the PID parameters with the set_param method.

Sign in to comment.

Sign in to answer this question.