A virtual signal is a signal that graphically represents other signals or parts of other signals. Virtual signals are purely graphical entities; they have no mathematical or physical significance. Simulink® ignores them when simulating a model, and they do not exist in generated code. Some blocks, such as the Mux block, always generate virtual signals. Others, such as Bus Creator, can generate either virtual or nonvirtual signals.
The nonvirtual components of a virtual signal are called regions. A virtual signal can contain the same region more than once. For example, if the same nonvirtual signal is connected to two input ports of a Mux block, the block outputs a virtual signal that has two regions. The regions behave as they would if they had originated in two different nonvirtual signals, even though the resulting behavior duplicates information.
Bus signals can also be virtual or nonvirtual. For details, see Virtual and Nonvirtual Buses.
A Simulink mux is a virtual signal that graphically combines two or more scalar or vector signals into one signal line. A Simulink mux is not a hardware multiplexer, which combines multiple data streams into a single channel. A Simulink mux does not combine signals in any functional sense: it exists only virtually, and its only purpose is to simplify the visual appearance of a model. Using a mux has no effect on simulation or generated code.
You can use a mux anywhere that you could use an ordinary (contiguous) vector. For example, you can perform calculations on a mux. The computation affects each constituent value in the mux just as if the values existed in a contiguous vector, and the result is a contiguous vector, not a mux. Using a mux to perform computations on multiple vectors avoids the overhead of copying the separate values to contiguous storage.
The Simulink documentation refers, sometimes interchangeably, to "muxes", "vectors", and "wide signals", and all three terms appear in Simulink GUI labels and API names. This terminology can be confusing, because most vector signals, which are also called wide signals, are nonvirtual and hence are not muxes. To avoid confusion, reserve the term "mux" to refer specifically to a virtual vector.
A mux is a virtual vector signal. The constituent signals of a mux retain their separate existence in every way, except visually. You can also combine scalar and vector signals into a nonvirtual vector signal, by using a Vector Concatenate block. The signal output by a Vector Concatenate block is an ordinary contiguous vector, inheriting no special properties from the fact that it was created from separate signals.
To create a composite signal whose constituent signals retain their identities and can have different data types, use a Bus Creator block rather than a Mux block. For details, see Overview of Composite Signal Techniques. Although you can use a Mux block to create a composite signal in some cases, MathWorks® discourages this practice. See Prevent Bus and Mux Mixtures for more information.
The Signal Routing library provides two virtual blocks for implementing muxes:
Combine several input signals into a mux (virtual vector) signal
Extract and output the values in a mux (virtual vector) signal
To implement a mux signal:
Select a Mux and Demux block from the Signal Routing library.
Set the Mux block Number of inputs and the Demux block Number of outputs block parameters to the desired values.
Connect the Mux, Demux, and other blocks as needed to implement the desired signal.
The next figure shows three signals that are input to a Mux block, transmitted as a mux signal to a Demux block, and output as separate signals.
The Mux and Demux blocks are the left and right vertical bars, respectively. To reduce visual complexity, neither block displays a name. In this example, the line connecting the blocks, representing the mux signal, is wide because the model has been built with Display > Signals & Ports > Wide Nonscalar Lines option enabled. See Display Signal Attributes for details.
Signals input to a Mux block can be any combination of scalars, vectors, and muxes. The signals in the output mux appear in the order in which they were input to the Mux block. You can use multiple Mux blocks to create a mux in several stages, but the result is flat, not hierarchical, just as if the constituent signals had been combined using a single mux block.
The values in all signals input to a Mux block must have the same data type.
If a Demux block attempts to output more values than exist in the input signal, an error occurs. A Demux block can output fewer values than exist in the input mux, and can group the values it outputs into different scalars and vectors than were input to the Mux block. However, the Demux block cannot rearrange the order of those values. For details, see Demux.
Note: MathWorks discourages using Mux and Demux blocks to create and access buses under any circumstances. See Prevent Bus and Mux Mixtures for details.