mulitlevel structure does not work with simulink bus

2 views (last 30 days)
Hi,
I need to work with highly complex structures in simulink. I have a main m-file in which I create a bus object in the main workspace:
main.sub(1).a = 2;
main.sub(1).b = 3;
main.sub(2).b = 4;
bus1 = Simulink.Bus.createObject(main);
In simulink I have a matlab function, that looks like this:
function main = set_new_structure_values(u)
main.sub(1).a = u(1);
main.sub(1).b = u(2);
main.sub(2).b = u(3);
end
In the bus editor, I set "slBus1" as name and the type bus for the function output.
But it just doesnt work, I get errors like this: "Errors occurred during parsing of MATLAB function 'MATLAB Function'(#48)" It seems to has something to do with the index "(i)", but I am not sure.
I dont know why this doesnt work. Documentation is not very helpfull here. Does anyone have an idea? Thanks in advance.

Answers (1)

Tamir Suliman
Tamir Suliman on 23 Feb 2023
Edited: Tamir Suliman on 23 Feb 2023
check the input and output data types of the MATLAB function block. The input should be a vector of the same length as the number of fields in the bus, and the output should be a bus of the same type as the bus object you created in the main m-file.
make sure that the bus object is defined correctly in the main m-file. Make sure that the names and data types of the bus elements match those in the MATLAB function.
function out = set_new_structure_values(in)
out.sub(1).a = in(1);
out.sub(1).b = in(2);
out.sub(2).b = in(3);
end

Products

Community Treasure Hunt

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

Start Hunting!