Main Content

UseSimulink.SignalObjects to Specify and Control Signal Attributes

ASimulink.Signalobject enables you to assign or validate the attributes of a signal or discrete state, such as its data type, numeric type, dimensions, and so on.

Using Signal Objects to Assign or Validate Signal Attributes

您可以使用signal objects to assign or validate signal attributes. The same techniques work with discrete states also. To use a signal object to assign or validate signal attribute values:

  1. Create aSimulink.Signalobject that has the same name as the signal to which you want to assign attributes or whose attributes you want to validate.

    1. Open the Model Explorer.

    2. In the Model Hierarchy pane, select either the Base workspace or Model workspace node, depending on the context you want for the signal object. If you create the signal object in a model workspace, you must set theStorage classparameter toAuto.

    3. SelectAdd>Simulink Signal.

  2. Set the properties of the object that correspond to the attributes left unspecified by the signal source, or that correspond to the attributes you want to validate.

  3. Enable explicit or implicit signal resolution:

    • Explicit resolution:In the Signal Properties dialog box for the signal, enableSignal name must resolve to Simulink signal object. This is the preferred technique. SeeExplicit and Implicit Symbol Resolutionfor more information.

      When you use this technique, setConfiguration Parameters > Diagnostics > Data Validity > Signal resolutionto a value other thanNone. To use only explicit resolution (a best practice), set the parameter toExplicit only.

    • Implicit resolution:Set theConfiguration Parameters > Diagnostics > Data Validity > Signal resolutionoption for the model toExplicit and implicitorExplicit and warn implicit. Explicit resolution is the preferred technique.

  4. Assign the signal object to a workspace variable.

  5. Associate the signal object with the source signal.

Validation

The result when a signal does not match a signal object can depend on several factors. Simulink®software can validate a signal property when you update the diagram, while you run a simulation, or both. When and how validation occurs can depend on internal rules that are subject to change, and sometimes on configuration parameter settings.

Not all signal validation compares signal source attributes with signal object properties. For example, if you specifyMinimumandMaximumsignal values using a signal object, the signal source must specify the same values as the signal object (or inherit the values from the object) but such validation relates only to agreement between the source and the object, not to enforcement of the minimum and maximum values during simulation.

If the value ofConfiguration Parameters > Diagnostics > Data Validity > Simulation range checkingisnone(the default), Simulink does not enforce any minimum and maximum signal values during simulation, even though a signal object provided or validated them. To enforce minimum and maximum signal values during simulation, setSimulation range checkingtowarningorerror. SeeSpecify Signal RangesandModel Configuration Parameters: Data Validity Diagnosticsfor more information.

Multiple Signal Objects

您可以将一个给定的关联signal objectwith more than one signal if the storage class of the signal object isAutoorReusable. If the storage class isAutoand you clear optimizations such asSignal storage reuseso that the generated code allocates memory for all of the associated signals, the signals each appear as a uniquely named field of the global structure that contains signal and state data. If the storage class of the object is other thanAutoorReusable, you can associate the signal object with no more than one signal.

您可以将一个给定的关联signalwith no more than one signal object. The signal can refer to the signal object more than once, but every reference must resolve to exactly the same signal object. Referencing two different signal objects that have exactly the same properties causes a compile-time error.

A compile-time error occurs if a model associates more than one signal object with any signal. To prevent the error, decide which object you want the signal to use, then delete or reconfigure all references to any other signal objects, so that all remaining references resolve to the chosen signal object. SeeHighlight Signal Sources and Destinationsfor a description of techniques that you can use to trace the full extent of a signal.

信号规范块:另一种选择Simulink.Signal

您可以使用aSignal Specificationblock rather than aSimulink.Signalobject to assign properties left unspecified by a signal source. Each technique has advantages and disadvantages:

  • Using a signal object simplifies the model and allows you to change signal property values without editing the model, but does not show signal property values directly in the block diagram.

  • Using aSignal Specificationblock displays signal property values directly in the block diagram, but complicates the model and requires editing it to change signal property values.

The following two models illustrate the respective advantages of the two ways of assigning attributes to a signal.

In the first example, the signal object namedSig1specifies the sample time and data type of the signal emitted by input portIn1.

To determine the properties of theSig1signal, you can view the signal object in the Model Explorer. In this model, the sample time is-1and the data type isauto.

Using a signal object to specify the sample time and data type properties of signalSig1allows you to change the sample time or data type without having to edit the model. For example, you could use the Model Explorer, the MATLAB®command line, or a MATLAB program to change these properties.

The second example uses aSignal Specificationblock specifies the sample time and data type of the signal emitted by input portIn2. TheSignal Specificationblock displays the data type and signal sample time properties right in the diagram, which in this case areuint8and4, respectively.

Bus Support

Using Bus Objects as the Data Type

Simulink.Signalsupports nonvirtual buses as the output data type.

If you set theData typeof the signal object to be a bus object, then you cannot associate the signal object with a non-bus signal.

Using Structures for the Initial Value

If you use a bus object as the data type, setInitial valueto0or a MATLAB structure that matches the bus object.

The structure you specify must contain a value for every element of the bus represented by the bus object.

您可以使用theSimulink.Bus.createMATLABStructto create a full structure that corresponds to a bus.

您可以使用Simulink.Bus.createObjectto create a bus object from a MATLAB structure.

See Also