Input hierarchical varying length but constant structure into a MATLAB function within Simulink
3 views (last 30 days)
Show older comments
Hi,
I have a hierarchical data structure with lots of sub structures with varying types within them and differing lengths. It represents real aircraft test data and so is not a uniform data set. I need to pass this large structure from a workspace (Matlab or simulink) into my matlab function block within simulink. I've read a lot of different posts on this issue but getting myself very confused so here's a minimum case that produces my error:
Just a lone Matlab function block (in simulink) - With basic set as an inherited parameter in the property inspector with tunable turned off.
function fcn(basic)
basic
In the MATLAB workspace define the basic structure:
basic.deep(1).filename = "test1"; basic.deep(2).filename = "longerString";
Running this provides this error in 2022b:
Mixed field types in structure arrays are not supported. Type at 'basic.deep(1).filename' differed from type at 'basic.deep(2).filename'. Parameter Data 'basic'
Component:MATLAB Function | Category:Coder error
Mixed field types in structure arrays are not supported. Type at 'basic.deep(1).filename' differed from type at 'basic.deep(2).filename'. Parameter Data 'basic'
Despite them both being strings.
For reference my real structure has 29 sub structures each with a Table and sub-sub structure with two fields and variable lengths of data. As this is aircraft data it is constant with time.
I've tried a lot of different ways of importing structures beyond this, trying to create bus objects, trying the data inputs on the model configuration etc. This seems like the right and simplest way so any help would be appreciated!
1 Comment
Yash Sharma
on 22 Mar 2024
Hi Joshua,
I believe you are receiving this error due to the varying lengths of the strings. Consider using a cell array instead of strings to address this issue.
Answers (1)
Pramil
on 11 Sep 2024
Hi Joshua,
The error you are getting is expected, since the length of both the strings are different, the compiler interprets them as different structures.
You can resolve the issue by either doing one of the following:
- Use same size character arrays instead of strings. You can decide a maximum array length and add trailing spaces in rest.

- Use datatypes supported for code generation. You can refer the following documentations for the same: https://www.mathworks.com/help/releases/R2022b/coder/getting-started-with-matlab-coder.html
And here is the list of datatypes supported for code generation:
I hope this resolves your issue.
0 Comments
See Also
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!