Matlab - Maple Help

Home : Support : Online Help : Mathematics : Numerical Computations : Matlab : Matlab/defined

Matlab

 defined
 check the existence of a variable in an open MATLAB(R) session

 Calling Sequence defined(M, attribute)

Parameters

 M - string naming the MATLAB® variable to be tested attribute - (optional) where attribute is one of 'variable', 'function', or 'globalvar'

Description

 • The command defined determines whether the variable M is defined in MATLAB®. By default, when no attribute parameter is specified, the defined command checks whether a variable named M exists in the MATLAB® environment.
 • If the command contains the optional parameter 'function', then defined returns a value of 'true' only if a function named $M$ is defined in the MATLAB® environment. The option 'globalvar' returns 'true' if the variable M is defined in the MATLAB® environment and it is a global variable.
 • To set a global variable in MATLAB®, define the variable as a global by using Matlab[setvar], with the optional parameter 'globalvar' as in the calling sequence Matlab[setvar]("M", value, 'globalvar'). Variables must be declared as global to work with functions that use global variables of the same name.
 • The defined command establishes whether M exists in the MATLAB® session.  Executing the defined command returns either 'true' or 'false'.

Examples

Setting a Maple matrix in MATLAB®.

 > $\mathrm{with}\left(\mathrm{Matlab}\right):$
 > $\mathrm{maplematrix_a}≔\mathrm{LinearAlgebra}[\mathrm{RandomMatrix}]\left(2\right)$
 > $\mathrm{Matlab}[\mathrm{setvar}]\left("matlabmatrix_a",\mathrm{maplematrix_a}\right)$

The existence of the matrix in MATLAB® is now checked.

 > $\mathrm{Matlab}[\mathrm{defined}]\left("matlabmatrix_a"\right)$

 true

Note here that the matrix defined in Maple is not defined in MATLAB®

 > $\mathrm{Matlab}[\mathrm{defined}]\left("maplematrix_a"\right)$

 false

This example assumes that a MATLAB® function has been saved in the file example.m

 % multiply the input value x by the global variable y function ret=example(x) global y ret=x*y;

 > $\mathrm{Matlab}[\mathrm{setvar}]\left("y",10.123,'\mathrm{globalvar}'\right)$
 > $\mathrm{Matlab}[\mathrm{defined}]\left("y",'\mathrm{globalvar}'\right)$

 true

 > $\mathrm{Matlab}[\mathrm{defined}]\left("example",'\mathrm{function}'\right)$

 true

 > $\mathrm{Matlab}[\mathrm{defined}]\left("y",'\mathrm{variable}'\right)$

 true

 > $\mathrm{Matlab}[\mathrm{evalM}]\left("answer = example\left(2\right)"\right)$
 > $\mathrm{Matlab}[\mathrm{getvar}]\left("answer"\right)$

 20.246