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

回答(1 个)

Naty S
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
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
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 CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by