matlab editor to simulink communication

I have script that runs over 2000 iterations. Inside the loop i constantly update two variables say x and y whose size is 1 by N where is varying. the size of x is always equal to size of y. I need to export these x and y values to simulink for every iteration how do i do that?
my algorithm:
%some code
.
.
.
for i = 1:1:2000
%some code
....
N = randi(100);
x = rand(1,N);
y = rand(1,N);
%for every iteration I need to send the x and y vectors to simulink
end
Thanks in advance

2 Comments

Are you running a complete model each time? Or are you stepping the model for (say) 0.1 seconds each time?
I'm running the complete model. But once I start my script, one iteration takes around 1second to complete. So I would need to send the x and v vectors every second.

Sign in to comment.

Answers (1)

Write x and y into the base workspace, and use a From Workspace block in the model, which you would invoke using sim()
However, From Workspace is mostly for importing a signal -- something that has time information attached to it, with particular values to be released as signal inputs at particular times.
If what you have is instead arrays to be used in calculations, then instead of using From Workspace, use set_param() at the MATLAB level to set block properties. See https://www.mathworks.com/matlabcentral/answers/1653710-global-variable-in-simulink#comment_2009495 for some functions to construct real simulink signals (function RSS), real simulink parameters (function RSP) or complex simulink signals (function CSS)

3 Comments

I tried using from workspace and I encountered two problems. 1) since the size of x and y varies every iteration, from workspace block doesn't accept the structure in any format. It throws an error saying the format not allowed.
2) I need to run the script and wait for the 2000 iterations to complete and then run simulink to export the data. I'll check the rest.
From Workspace of numeric values requires a 2D array, with the first column being treated as the time.
Using from workspace block, I am able to export one set of vectors to simulink.
Here is what I do:
>I run the script, wait for iteration 1 to complete so that x and y vectors are populated.
>I stop the script.
>in simulink I have two from workspace blocks one for x and one for y. I also have a matlab function block to check the received data.
>I run the simulink model and I am able to get the x and y vectors.
Here is what I tried:
>Since the x and y are updated for every iteration, I run the script for N iterations and stored x and y vectors in cell arrays. each cell of x and y contains data of size 1 by k. I tried exporting this the same way i tried before but unsuccessful.
>I run the script for N iterations and store the data in structures but since the x and y gets updated everytime the structure (variable name).signals.values has multiple fields and i am unable to export this aswell.

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Asked:

on 11 Sep 2023

Commented:

on 12 Sep 2023

Community Treasure Hunt

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

Start Hunting!