MATLAB Answers

Way to Iterate through struct fields for code generation in Matlab Embedded Function for Simulink

2 views (last 30 days)
Thomas Marullo
Thomas Marullo on 17 Jul 2014
I have a Struct variable being passed into my Matlab Function inside Simulink, ie:
MyStruct.Field1.(some more fields)
MyStruct.Field2.(some more fields)
...
MyStruct.FieldN.(some more fields)
Each field has a different structure to it so I had created a Bus to allow Simulink to deal with the data and make each field explicitly since Simulink does not allow a Struct array with different field types, but that's besides the point.
My problem is that inside of my Matlab Embedded Function I want to be able to iterate through my Fields since the number of fields for my models can change and I will be calling a subroutine for each one. Currently, I have to explicitly rewrite my embedded function, ie:
subroutine(MyStruct.Field1)
subroutine(MyStruct.Field2)
...
subroutine(MyStruct.FieldN)
Ideally, I wanted to do something like this, but it doesn't work because code generation doesn't allow for it.
numFields = length(fieldnames(MyStruct);
for i = 1:numFields
subroutine(MyStruct.(sprintf('Field%d',i));
end
Problem is that 'fieldnames' and 'sprintf' is not supported for code generation. So I'm stuck looking for another solution.

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!