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!