0

I am trying to create an array of structures in Simulink and got some problems with it. first of all i tried to create it directly in Simulink using this:

function a = fcn(Dibhole, t , x, const)
%#codegen
%Output = zeros(10,10);
f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
a = repmat(cube, 20, 10);
for i = 1:20
    for j = 1:10
        a(i,j).number = 0;
        a(i,j).move   = 0;
    end
end

and i got 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 'a' and set data type for 'a' to be 'Bus: ', where '' is the name of a bus object from the MATLAB workspace.

So i found some example how to create struct in Matlab and receive this to Simulink: http://blogs.mathworks.com/seth/2011/12/05/initializing-buses-using-a-matlab-structure/ That works perfectly but i still can't repeat this with array:

f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
myStruct2 = repmat(cube, 20, 10);
for i = 1:20
    for j = 1:10
        myStruct2(i,j).number = 1;
        myStruct2(i,j).move   = 1;
    end
end

busInfo = Simulink.Bus.createObject(myStruct2);

Can anyone clarify to me what's the problem? Or maybe there is different way to create array of struct in Simulink?

Mihail

2 Answers 2

1

Simulink wants you to define the output of the function to be a bus.

As 'Bus: My_test_bus', for example.

Take a look at the Simulink Bus Editor. You can find it in any model under the menu, Edit->Bus Editor.

This would be a good start.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, Rick Now i got understanding of this item. All my problems was there is no way to put structure into Bus. so to use array of structures I need to define it in MATLAB function and create global Bus Signal of the same type: num1 = ones(15,15,15); move1 = ones(15,15,15); a1 = struct('number',num1,'movement', move1); arr = [ a a1]; busInfo1 = Simulink.Bus.createObject(arr); This creates slBus in global variables. Then define output data type for any MATLAB function like slBus. All troubles are - I can't use Bus Selector, I must create one more MATLAB function to work with it's fields
0

Rick, i think you are right! i have tried this problem for a long time, and have got this results:

the irony is that I was never able to create array of structures BUT i did this with structure of arrays! :D

I made this steps for it:

  1. to use structure of arrays we need to define and initialize it in some MATLAB function. Like this:

    number = zeros(10,1);
    move   = zeros(10,1);
    
    for i = 1:10
    
        number(i,1) = i+1;
        move(i,1)   = i+2;
    end
    
    a = struct('numbers',number,'movement', move);
    
  2. To work this data we must use Bus Selector.

So we have array in "numbers" and "movement".

  1. BUT! Here we go, Rick: we must define type of output of MATLAB function like Bus! How to do this in simulink? i found this way: in model properties in simulink Callbacks/PreLoadFcn define some function and in same folder as project create .m file named like this just defined function. In this file create structure of array and define Bus type for it:

    number = zeros(10,1);
    move   = zeros(10,1);
    
    a = struct('numbers',number,'movement', move);
    
    busInfo = Simulink.Bus.createObject(a);
    

Now we have Bus type for our structure at first loading of simulink model.

  1. Last step: define MATLAB function output type directly. in Model Explorer choose your MATLAB function. choose output variable. Set DataType for it: Bus:slBus1 (the name of this Bus type you can see in wokspace of matlab, because its a global variable).

That's all! now it works!

(tried to add pictures, but i have no enough reputation :( )

Now my program works in this way, but i also tried to create array of structures and still have the problems. i tried to create Bus for it, but can't transmit it to Bus Selector - it doesn't know what to do with structures... i also tried to add one more MATLAB function to create some data from structures and then display it, but it doesn't works too(

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.