Can't use structs as outputs from simulink models

20 views (last 30 days)
Hello,
After a few tries and reading in matlab forums i found out that it is not possible to use struct as output, and the other options is to insert struct in to bus.
While there is a lot of mentiones about it, there is not good example on how to do it.
I am using matlab function model which is under user-defined library, and use the following function:
function simconf = fcn
Interetions = 2;
simconf.Interetions = Interetions;
end
I get this error: "Derived output was of type struct. 'Inherited type' is unsupported for this type and a defined bus object must be used instead. Click on 'simconf' and set data type for 'simconf' to be 'Bus: <object name>', where '<object name>' is the name of a bus object from the MATLAB workspace."

Answers (1)

Githin George
Githin George on 9 Feb 2024
Hello,
The error message that you have provided indicates that “struct” objects cannot be the output of a “MATLAB function” block in Simulink. You can change the output type of the function block to a “Simulink.Bus” object to correct this issue. For this task you will have to define your “Simulink.Bus” object either in base workspace or in the interactive “typeeditor”. The following code creates a “Bus” object in the workspace with the property ‘Interetions’
elems(1) = Simulink.BusElement;
elems(1).Name = 'Interetions';
TopBus = Simulink.Bus;
TopBus.Elements = elems;
Once you have created the Bus object, open the model explorer, and navigate to the “MATLAB function” block listed under your model. In the datatype section labelled “Type”, you can update the value with “Bus: TopBus” to assign the datatype correctly. Now you can use the “Bus” object without issues in the “MATLAB function” block. You can have a look at the attached screenshot for better clarity on the workflow.
The following documentation links may prove helpful in better understanding these concepts.
I hope this helps.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!