Main Content

convertUnits

Convert units of Simulink.sdi.Signal object

Description

example

convertUnits(sig,units) converts the stored units of the Simulink.sdi.Signal object sig to the units specified by units. When you convert the stored units, the display units are updated. For a list of supported units, see Allowed Units. You can use the convertUnits function to convert the units of Simulink.sdi.Signal objects that contain data of all built-in and fixed-point types.

Note

Unit conversion does not support undo and may lead to precision loss.

Examples

collapse all

Use the convertUnits function to convert the stored units of a Simulink.sdi.Signal object. This example uses data generated by simulating the model sldemo_autotrans. When you convert the stored units of the Signal object, the Simulation Data Inspector performs a conversion on the data and updates the signal display units any plots that display the signal. Changing the stored units of a Signal object does not affect the model that created the signal.

Generate Simulation Data

Simulate the sldemo_autotrans model to create a run in the Simulation Data Inspector. Then, use the Simulink.sdi.Run.getLatest function to access the Run object that corresponds to the simulation.

out = sim('sldemo_autotrans');

autoRun = Simulink.sdi.Run.getLatest;

Inspect the Signal Properties

Get the Simulink.sdi.Signal object for the EngineRPM signal and check the StoredUnits and DisplayUnits properties.

engine_sig = getSignalsByName(autoRun,'EngineRPM');

engine_sig.StoredUnits
ans = 
'rpm'
engine_sig.DisplayUnits
ans = 
'rpm'

Convert Stored Units

Use the convertUnits function to convert the EngineRPM signal units to rad/s. When you convert the stored units, the display units are updated. Then, change the signal name to reflect the new units.

convertUnits(engine_sig,'rad/s')
engine_sig.Name = 'EngineFreq,rad/s';

Check the modified signal properties.

engine_sig.StoredUnits
ans = 
'rad/s'
engine_sig.DisplayUnits
ans = 
'rad/s'
engine_sig.Name
ans = 
'EngineFreq,rad/s'

Input Arguments

collapse all

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

Desired signal units, specified as a string or character vector. For a list of accepted units, see Allowed Units.

Example: 'm'

Example: "ft/s"

Data Types: char | string

Introduced in R2018a