matlab editor to simulink communication
2 views (last 30 days)
Show older comments
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
Walter Roberson
on 11 Sep 2023
Are you running a complete model each time? Or are you stepping the model for (say) 0.1 seconds each time?
Answers (1)
Walter Roberson
on 11 Sep 2023
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
Walter Roberson
on 11 Sep 2023
From Workspace of numeric values requires a 2D array, with the first column being treated as the time.
https://www.mathworks.com/help/simulink/ug/load-data-using-the-from-workspace-block.html describes the possibilities in more detail
See Also
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!