MATLAB: How to create an output bus with variable-size elements using a structure in Embedded MATLAB

emlsimulink

I would like to setup the Embedded MATLAB Function block to output a bus with variable dimensions. When I attempt to use 'eml.varsize' to define a specific signal within the bus to be variable-size, as shown in the attached model (incorrect.mdl), I receive the following errors:
struct {ab_diff: double, ab_sum: double} ~= struct {ab: double, ab_sum: double}. Structures don't match.
The struct to the left is the class of the left-hand side of the assignment.
Function 'Embedded MATLAB Function' (#47.146.152), line 12, column 1:
"outbus"
Launch diagnostic report.
Size of outbus(:).ab_diff can not be modified as the variable outbus is already locked. Place the eml.varsize() declaration before the first use of outbus.
Function 'Embedded MATLAB Function' (#47.195.214), line 13, column 13:
"'outbus(:).ab_diff'"
Launch diagnostic report.
Size mismatch (size [1 x 1] ~= size [1 x 3]).
The size to the left is the size of the left-hand side of the assignment.
Function 'Embedded MATLAB Function' (#47.223.237), line 14, column 1:
"outbus.ab_diff"
Launch diagnostic report.

Best Answer

To create a variable size bus using the command line, use the following steps:
- Create a bus element that is varsized.
1. Create a bus element using the Simulink.BusElement object. For more information consult the following documentation at the MATLAB Command prompt in MATLAB 7.10 (R2010a):
web([docroot '/toolbox/simulink/slref/simulink.buselement.html#bqqmehe-1'])
2. After specifying other properties of the bus element, such as name etc., set the following properties of the bus element:
a. Dimensions - use an array that specifies the dimension of this bus element. This is the maximum dimension.
b. DimensionsMode - 'Variable' - this will indicate that the bus element is varsized
- Add all the relevant bus element objects to a Simulink.Bus object as shown below:
ABus_Element=Simulink.BusElement
ABus_Element.Name='A'
BBus_Element=Simulink.BusElement
BBus_Element.Name='B'
BBus_Element.DimensionsMode='Variable'
BBus_Element.Dimensions=4 %for example
ABus=Simulink.Bus
ABus.Elements=[ABus_Element;BBus_Element]
To create a variable size bus using the Bus Editor, consult the following documentation at the MATLAB Command prompt in MATLAB 7.10 (R2010a):
web([docroot '/toolbox/simulink/ug/brin2jr-1.html#brin204-4'])
The attached example model (TS_example_varsize.mdl) demonstrates how one may use a bus that contains a variable size signal. Here, the following steps were taken:
1. Two buses are created in the PostLoadFcn callback of the model:
- InputBus - whose elements are fixed in size
- OutputBus - whose elements are variable in size, with a maximum dimension
2. Using a BusCreator block, the input bus signal is passed to an Embedded MATLAB block.
3. Using the Embedded MATLAB block's Port and Data Manager, the input and output are set to the bus objects mentioned in step 1.
4. Within the Embedded MATLAB block 'outbus.ab_sum' is defined as a vector of 1's whose length is equal to the rounded value of one of the inputs.
5. The output bus of the Embedded MATLAB block is then passed to a Bus Selector block that allows for each bus element to be displayed individually.
Note the following features of this model:
1. The PostLoadFcn callback declares the ab_sum bus element to have 'DimensionsMode'='Variable' and a 'Dimension'= 4. This is to create a varsized bus as discussed above.
2. The input 'b' for the Embedded function MATLAB block has been used to define the length of the signal ab_sum for a given time step.
3. Line 10 of the Embedded MATLAB function block ensures that the length of the variable size signal never violates the upper bound that was established in the PreLoadFcn callback
refer to the documentation for information regarding variable-size data in Embedded MATLAB code by executing the following at the MATLAB Command prompt in MATLAB 7.10 (R2010a):
web([docroot '/toolbox/eml/ug/br7rggy.html#br9t1u0'])
web([docroot '/toolbox/eml/ref/eml.varsize.html'])