Main Content

setVariablePart

Set property of variable in model workspace

Description

example

varValue = setVariablePart(mdlWks,varName.Property, varValue) assigns varValue to the MATLAB variable property varName.Property in the model workspace represented by the Simulink.ModelWorkspace object mdlWks.

Using setVariablePart is preferable to using evalin for assigning variable properties in the model workspace because the setVariablePart function does not create a new variable or cause unintended results.

Examples

collapse all

Open the example model vdp.

open_system('vdp')

Create a Simulink.ModelWorkspace object that represents the model workspace of vdp.

mdlWks = get_param('vdp','ModelWorkspace');

Create a structure named myStruct with fields a, b, and c.

aStruct.a = 10;
aStruct.b = {1,2,3,4,5};
aStruct.c = Simulink.Parameter(7);
mdlWks.assignin('myStruct',aStruct); 

Assign new values to the structure fields.

setVariablePart(mdlWks,'myStruct.a', 2);
setVariablePart(mdlWks,'myStruct.b{1}', 2);
setVariablePart(mdlWks,'myStruct.c', Simulink.Parameter(2));

Return the new values of the structure fields. Store the values in varValuea, varValueb, and varValuec.

varValuea = getVariablePart(mdlWks,'myStruct.a')
varValueb = getVariablePart(mdlWks,'myStruct.b{1}')
varValueC = getVariablePart(mdlWks,'myStruct.c.Value')

Input Arguments

collapse all

Target model workspace, specified as a Simulink.ModelWorkspace object.

Name of the target variable property, specified as a character vector.

Example: 'myVariable.Property'

Data Types: char | string

Value to assign to the value property, specified as a valid value. For example, you can specify a literal number, a structure, or an expression that evaluates to a valid value.

If you specify the name of a handle object, such as a Simulink.Parameter object, use the copy function to create a separate copy of the object.

Example: 5.12

Example: struct('a',5.12,'b',7.22)

Example: Simulink.Parameter(5.12)

Example: copy(myExistingParameterObject)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | categorical | datetime | duration | calendarDuration | fi
Complex Number Support: Yes

Introduced in R2018b