Accessing all values of a certain field inside a structure array in Simulink

5 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Naty S
Naty S il 22 Dic 2015
Amm, I didn't get an error with what you mentioned but maybe this will work:
test_struct(:).B
  3 Commenti
Naty S
Naty S il 23 Dic 2015
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 il 18 Feb 2020
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?

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by