Main Content

Simulink.data.assigninGlobal

Modify variable values in context of Simulink model

Description

Simulink.data.assigninGlobal(modelName,varName,varValue) assigns the value varValue to the variable varName within the context of the Simulink® model modelName. This function updates all instances of the variable varName in the data sources, including data dictionary and MAT files associated with the model modelName.

If the variable does not exist, the function creates the variable in the following manner:

  • If a data dictionary is linked to the model as an external data source, the function creates an entry in the Design Data section of the dictionary.

  • If both a MAT file and a data dictionary are linked to the model as external data sources, the function creates an entry in the Design Data section of the dictionary.

  • If only a MAT file is linked to the model as an external data source, the function creates an entry in the MAT file.

  • If multiple MAT files are linked to the model as external data sources, the function reports an error.

  • If no external data sources are linked to the model, the function creates a variable in the MATLAB® base workspace.

If the model is linked to a data dictionary that references other dictionaries, the function searches for the variable in the entire dictionary hierarchy. If it does not find a matching variable, the function creates an entry in the dictionary that is linked to the model.

example

Examples

collapse all

Assign a variable myNewVariable with value 237 in the context of the Simulink model vdp.slx, which has no external data sources linked.

openExample('simulink_general/VanDerPolOscillatorExample')
Simulink.data.assigninGlobal('vdp','myNewVariable',237)

myNewVariable appears as a variable in the MATLAB base workspace.

Assign a variable myNewVar with value true in the context of the model BasicModellingData.slx, which is linked to the MAT file BasicModellingDataDefinitions.mat.

openExample('BasicModellingData')
Simulink.data.assigninGlobal('BasicModellingData','myNewVar',true)

Confirm the addition of the variable myNewVar to the linked MAT file BasicModellingDataDefinitions.mat by viewing it in Model Explorer.

Assign a variable myNewEntry with value true in the context of the model sldemo_fuelsys_dd_controller.slx, which is linked to the data dictionary sldemo_fuelsys_dd_controller.sldd.

openExample('simulink_automotive/UseDDForFuelContSysExample')
sldemo_fuelsys_dd_controller
Simulink.data.assigninGlobal('sldemo_fuelsys_dd_controller',...
'myNewEntry',true)

The entry myNewEntry appears in the Design Data section of the dictionary.

Confirm the addition of myNewEntry to the data dictionary sldemo_fuelsys_dd_controller.sldd by viewing the dictionary in Model Explorer.

myDictionaryObj = Simulink.data.dictionary.open(...
'sldemo_fuelsys_dd_controller.sldd');
show(myDictionaryObj)

Input Arguments

collapse all

Model name, specified as a character vector or a string.

Example: 'myTestModel'

Data Types: char | string

Name of the variable, specified as a character vector or a string.

Example: 'myTargetVariable'

Data Types: char | string

Value to assign to variable, specified as a MATLAB expression that returns any valid data type or data dictionary content.

Example: 27.5

Example: myBaseWorkspaceVariable

Example: Simulink.Parameter

Version History

Introduced in R2015a