필터 지우기
필터 지우기

Can a structure variable be updated within MATLAB Function block in Simulink?

조회 수: 37 (최근 30일)
I am running a simulation of a physical system in Simlink using a MATLAB function block. In this regard, I have some parameters that are defined in a structure in MATLAB workspace. I passed that structure by defining it as a parameter to the MATLAB function block. However, the code in the block attempts to add more fields to that structure, but the Simulink won't allow me to do that, giving the following error:
new fields cannot be added when structure has been read or used
Is there a way I can dynamically update the structure within the Function block?
Thanks

채택된 답변

Suman
Suman 2024년 7월 24일
Hi Afaq,
There are some limitations to using variables in MATLAB function block as opposed to the way it would run in a MATLAB environment. These restrictions are likely imposed to allow effiecient C code generation.
One of the restrictions is that you cannot add fields to the struct on the go. Please read about other restrictions while using struct in MATLAB function block here: https://www.mathworks.com/help/simulink/ug/how-working-with-structures-is-different-for-code-generation.html
You can create a struct with all the fields and assign it to zero at the beginning and then go about assigning the fields.
function a = b(t)
struct = struct('f1', 0, 'f2', 1);
struct.f1=t;
a=struct.f2;
end
If you need to add fields to the struct, please refer to using Variable-sized block variables: https://www.mathworks.com/help/simulink/ug/declare-variable-size-inputs-and-outputs.html
I hope that helps!
  댓글 수: 1
afaq ahmad
afaq ahmad 2024년 7월 24일
Hi Suman,
Thanks for your response. Actually, I had found a way around by using the S-function block instead of Matlab function block. S-function block is slow, but it gets the job done.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by