Accessing all values of a certain field inside a structure array in Simulink
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi everyone!
I have the following problem: I want to access all the values of a certain field inside a structure array with a user-defined function in Simulink. The structure array is a non-tunable parameter of the user-defined function. Here's an example of the structure array:
test_struct(1) = struct('A', 1, 'B', 2);
test_struct(2) = struct('A', 3, 'B', 4);
Now, when I try for example to only access the value of the field B via Matlab, I get:
>> [test_struct.B]
ans =
2 4
However, if I try to do the same with a user-defined function in Simulink:
function y = fcn(test_struct)
y = [test_struct.B];
end
I get the error message 'Referencing a component from array of non-scalar values is not supported for code generation.'
If I try to access the values of the structure array one by one, I get no errors in Simulink:
test_struct(1).B
test_struct(2).B
However, I would prefer not to access the values one by one since I do not want to use a loop. So how can I access all the values at once without getting the error?
Best, Ferros
댓글 수: 0
답변 (1개)
Naty S
2015년 12월 22일
Amm, I didn't get an error with what you mentioned but maybe this will work:
test_struct(:).B
댓글 수: 3
Naty S
2015년 12월 23일
Yeah, so once i have placed it in a user defined function i did get the same error. Don't really have a solution for you. I have noticed that if you place the variable in a constant and input the constant into the function it works (In constant use [test_struct(:).B] ) but i don't know if that is an option for you. really weird though
Mohammed Kagalwala
2020년 2월 18일
Hi,
I currently get the same error when trying to logically index into a struct of the same setup as test_struct, i.e.
test_struct([true false]).B
I was wondering if you had found a solution to your problem?
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!