matlab editor to simulink communication

조회 수: 1 (최근 30일)
Praanesh Sambath
Praanesh Sambath 2023년 9월 11일
댓글: Praanesh Sambath 2023년 9월 12일
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
Walter Roberson
Walter Roberson 2023년 9월 11일
Are you running a complete model each time? Or are you stepping the model for (say) 0.1 seconds each time?
Praanesh Sambath
Praanesh Sambath 2023년 9월 11일
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.

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

답변 (1개)

Walter Roberson
Walter Roberson 2023년 9월 11일
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
Walter Roberson
Walter Roberson 2023년 9월 11일
From Workspace of numeric values requires a 2D array, with the first column being treated as the time.
Praanesh Sambath
Praanesh Sambath 2023년 9월 12일
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.

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

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by