Did you notice that since R2010a it is possible to initialize buses with a MATLAB structure? If you were not aware, here is how it works.
Starting from a Structure
myStruct.a = 5;
myStruct.b = true;
myStruct.c.x1 = 22;
myStruct.c.x2 = 3;
busInfo = Simulink.Bus.createObject(myStruct);
Then I can configure a Constant block to use this bus object as data type and to use myStruct as value.
Starting from a model
Let's say I have the following model where a bus signal is going through a Unit Delay block. I want to specify different initial values for the elements of the bus.
In that case I can use the handle to a port of the Unit Delay block as input to Simulink.Bus.createMATLABStruct. This will create a MATLAB structure with the same hierarchy as the bus, filled with zeros. I can then overwrite fields of this structure as needed and use it as the initial value of the Unit Delay.
portHandle = get_param(gcb,'PortHandles');
initStruct = Simulink.Bus.createMATLABStruct(portHandle.Inport);
initStruct.bus2.Pulse = 5;
initStruct.signal1.Chirp = 3;
Requirements to Use Bus Signal Initialization
To enable bus signal initialization, you need to set two Configuration Parameter diagnostics:
- Mux blocks used to create bus signals to error.
- Underspecified initialization detection to simplified.
Now it's your turn
10 CommentsOldest to Newest
ExternalInputs_MATLABStruct = struct; ExternalInputs_MATLABStruct.VehControls = struct; ExternalInputs_MATLABStruct.VehControls.VehDrivingInputs = struct; ExternalInputs_MATLABStruct.VehControls.VehDrivingInputs.TBCInput = struct; ExternalInputs_MATLABStruct.VehControls.VehDrivingInputs.TBCInput(1, 1).Modes = 0; ExternalInputs_MATLABStruct.VehControls.VehDrivingInputs.TBCInput(1, 1).Values = 0;and so forth for the rest of the elements. Once I create the structure, I am trying to use it to initialize a signal of the same bus data type. I am doing so by specifying the structure name created above as the InitialValue field of the signal.
Inputs = Signal with properties: CoderInfo: [1x1 Simulink.CoderInfo] Description: '' DataType: 'Bus: ExternalInputs' Min:  Max:  DocUnits: '' Dimensions: -1 DimensionsMode: 'Fixed' Complexity: 'real' SampleTime: -1 SamplingMode: 'Sample based' InitialValue: 'ExternalInputs_MATLABStruct'When I try to build the model it gives the below error:
Data type of InitialValue 'ExternalInputs_MATLABStruct' must be double or the same as the signal object data typeAny inputs?