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
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。