Main Content

collapse

Represent multidimensional signal as a single signal with nonscalar sample values

Since R2021b

    Description

    collapse(sigObj) converts the representation of the signal that corresponds to the Simulink.sdi.Signal object sigObj from a set of signals with scalar sample values to a single signal with nonscalar sample values.

    When you convert the representation of a multidimensional signal from a set of scalar signals, called channels, to a single signal with nonscalar sample values:

    • The Signal object that previously contained the data for the first channel contains the data for the multidimensional signal.

    • The Signal objects for all other channels no longer contain data.

    • The signal IDs for all other channels become invalid.

    example

    Examples

    collapse all

    When you analyze multidimensional signal data using the Simulation Data Inspector, you can choose whether to represent the data as a single signal with multidimensional sample values or as a set of signals, called channels, with scalar sample values. Use the Simulation Data Inspector programmatic interface to convert the representation of signal data from channels to a multidimensional signal or from a multidimensional signal to channels.

    Open and Simulate the Model

    Open the model ConvertMultiDim. Then, simulate the model.

    mdl = "ConvertMultiDim";
    open_system(mdl);
    sim(mdl);

    Access the Run and Signal Data

    By default, the output of the Mux block is represented as channels in the Simulation Data Inspector because it contains fewer than four channels, so the run contains two signals: one for each channel in the Mux block output.

    Use the Simulink.sdi.getCurrentSimulationRun function to access the Simulink.sdi.Run object that contains the simulation data. Then, check how many Simulink.sdi.Signal objects the Run object contains using the SignalCount property on the Run object.

    runObj = Simulink.sdi.getCurrentSimulationRun(mdl);
    runObj.SignalCount
    ans = int32
    
    3
    

    Signals with multidimensional data have a top-level Signal object that does not contain data. The Children property of the top-level signal object contains one or more Signal objects associated with the top-level composite signal that contain the data. Because the top-level signals do not contain data, they are not counted to determine the value of the SignalCount property for the Run object.

    Use the getSignalsByName function to access the top-level Signal object for the Mux block output.

    topSig = getSignalsByName(runObj,"MuxSig");

    Use the Children property to access the Signal object for each channel.

    sig1 = topSig.Children(1);
    sig1.Name
    ans = 
    'MuxSig(1)'
    
    sig2 = topSig.Children(2);
    sig2.Name
    ans = 
    'MuxSig(2)'
    
    sig3 = topSig.Children(3);
    sig3.Name
    ans = 
    'MuxSig(3)'
    

    Convert Channels to a Multidimensional Signal

    Use the collapse function to convert the representation of the Mux block output so you access the data as a single signal with multidimensional sample values.

    collapse(sig1)

    The Run object now contains only one signal with multidimensional sample values.

    runObj.SignalCount
    ans = int32
    
    1
    

    The Children property of the top-level Signal object now contains only one signal.

    size(topSig.Children)
    ans = 1×2
    
         1     1
    
    

    After you convert a signal from channels to multidimensional representation, the Signal object that previously contained the data for the first channel contains the data for the multidimensional signal.

    sig1.Values.Data
    ans = 101×3
    
        1.1650         0         0
        0.6268    0.0631    0.0998
        0.0751    0.1265    0.1987
        0.3516    0.1899    0.2955
       -0.6965    0.2531    0.3894
        1.6961    0.3157    0.4794
        0.0591    0.3776    0.5646
        1.7971    0.4383    0.6442
        0.2641    0.4975    0.7174
        0.8717    0.5550    0.7833
          ⋮
    
    

    After conversion, the signal IDs for the Signal objects that contained data for other channels become invalid.

    isValidSignalID(runObj,sig2.ID)
    ans = logical
       0
    
    

    Convert a Multidimensional Signal to Channels

    Use the expand function to convert the Mux output back to channels.

    expand(sig1)

    After converting a multidimensional signal to channels, the Signal object that previously contained the data for the multidimensional signal contains the data for the first channel. New Signal objects are created for other channels.

    sig1.Values.Data
    ans = 101×1
    
        1.1650
        0.6268
        0.0751
        0.3516
       -0.6965
        1.6961
        0.0591
        1.7971
        0.2641
        0.8717
          ⋮
    
    

    The Run object and the Children property of the top-level Signal object both contain three signals.

    runObj.SignalCount
    ans = int32
    
    3
    
    size(topSig.Children)
    ans = 1×2
    
         1     3
    
    

    Access the new Signal object that contains the data for the second channel.

    newSig = topSig.Children(2);
    newSig.Name
    ans = 
    'MuxSig(2)'
    

    Input Arguments

    collapse all

    Channel of multidimensional signal to convert to a single signal, specified as a Simulink.sdi.Signal object.

    Version History

    Introduced in R2021b