Main Content

convertDataType

Convert data type for signal in Simulation Data Inspector

    Description

    example

    convertDataType(sigObj,dataType) converts the data in the Simulink.sdi.Signal object sigObj to the specified data type dataType.

    The convertDataType function supports converting to all built-in data types. For more information, see Data Types Supported by Simulink. When you have a license for Fixed-Point Designer™, you can also convert to fixed-point data types defined using the fixdt function.

    You cannot convert the data type for a signal produced by logging simulation data while the simulation that produces the signal is in progress.

    Warning

    You cannot undo a data type conversion in the Simulation Data Inspector. Some data types allow more precision in the stored data values than others. Conversion to a data type with less precision can result in permanent loss of precision in data values.

    Examples

    collapse all

    Simulate the model vdp while logging outputs using the Dataset format.

    mdl = "vdp";
    open_system(mdl)
    out = sim(mdl,SaveOutput="on",SaveFormat="Dataset");

    Access the Simulink.sdi.Run object that contains the logged data and metadata in the Simulation Data Inspector.

    run1 = Simulink.sdi.getCurrentSimulationRun(mdl);

    Access the Simulink.sdi.Signal object that contains the data for the signal x1.

    x1Sig = getSignalsByName(run1,"x1");

    Check the DataType property for the signal. The model uses the double data type for the signal.

    x1Sig.DataType
    ans = 
    'double'
    

    Convert the data type for the signal from double to single. You cannot undo a data type conversion in the Simulation Data Inspector. When you convert to a data type with less precision in the stored data values, precision lost due to conversion is permanent. A warning occurs when the data type conversion causes a loss in precision.

    convertDataType(x1Sig,"single");
    Warning: Changing the data type of signal 'x1' from 'double' to 'single' causes precision loss that cannot be undone.
    

    Check the DataType property for the signal again.

    x1Sig.DataType
    ans = 
    'single'
    

    Create time and signal data that represents a sine wave.

    time = 0.01*(0:1000);
    time = time';
    data = 50*sin(2*pi/3*time);

    By default, MATLAB® creates double data. To represent a scenario where another process creates int32 data you want to analyze in the Simulation Data Inspector, convert the data type for the signal values to int32.

    data = cast(data,"int32");

    Create a timeseries object using the time and data values for the signal. Then, name the timeseries object Sine Wave.

    sigTS = timeseries(data,time);
    sigTS.Name = "Sine Wave";

    Import the signal into the Simulation Data Inspector.

    runObj = Simulink.sdi.Run.create("Imported Data","vars",sigTS);

    Access the Simulink.sdi.Signal object with the data values. Then, check the signal data type.

    sigObj = getSignalsByName(runObj,"Sine Wave");
    sigObj.DataType
    ans = 
    'int32'
    

    Use the convertDataType function to convert the data type to double. Then, check the signal data type again.

    convertDataType(sigObj,"double");
    sigObj.DataType
    ans = 
    'double'
    

    Input Arguments

    collapse all

    Signal with data to convert, specified as a Simulink.sdi.Signal object.

    Desired data type for signal, specified as a string or a character vector.

    You can convert to any built-in data type by specifying the name for the data type as a string or character vector. For example, to convert to an 8-bit unsigned integer data type, specify the data type as "uint8". For more information about built-in data types and their names, see Data Types Supported by Simulink.

    When you have a license for Fixed-Point Designer, you can also convert to fixed-point data types defined using the fixdt function. To convert to a fixed-point data type, specify the fixdt command as a string or a character vector.

    Example: "int16" specifies conversion to the built-in signed 16-bit integer data type

    Example: "fixdt(1,12,4)" specifies conversion to a fixed-point data type defined using the fixdt function

    Version History

    Introduced in R2022a