主要内容

hasVariable

Determine whether variable exists in the model workspace of a model

Description

varExists = hasVariable(mdlWks,varName) returns 1 if a variable whose name is varName exists in the model workspace represented by the Simulink.ModelWorkspace object mdlWks.

example

Examples

collapse all

Open the example model vdp.

openExample('simulink_general/VanDerPolOscillatorExample')

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

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

Create a variable named myVar in the model workspace.

assignin(mdlWks,'myVar',5.12)

Determine whether a variable named myVar exists in the model workspace.

exists = hasVariable(mdlWks,'myVar')
exists =
   1

Input Arguments

collapse all

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

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

Example: 'myVariable'

Data Types: char

Output Arguments

collapse all

Indication of variable existence, returned as 1 (true) or 0.

Alternatives

Determine if specified variables exist in a model workspace by using a Simulink.data.DataConnection object function.

mw = Simulink.data.connect("vdp.slx");
mw.a = 1;
mw.c = 3;
exist(mw,["a" "b" "c"])
ans = 

  1×3 logical array

   1   0   1

Version History

Introduced in R2012a