How to check if a variable is defined in Octave?

When writing a script that loads data, it is a waste of time to wait for it to load every time.

How to check if a variable is defined?

+7
source share
3 answers

You can use the exist function in Octave to do this job. It can be used to verify the existence of a given name as a variable, built-in function, file or directory. In your case, to check for the existence of a variable, you can use something like this:

 if (exist("your_var_name", "var") == 1) printf("varname exists"); else printf("varname not exists"); endif 

For more information, you can refer to the following links:

+12
source

You must also put the variable name in quotation marks,

exist ("varname", "var")

+5
source
 if (exist("itemcount") == 1) % here it checks if itemcount is a variable, by changing the value after ==, you can check for function name, file name, dir, path etc. end 

Note: the item element is in double quotation marks.

By changing the value after ==, you can check the function name, file name, dir, path, etc.

from / more information at: https://www.gnu.org/software/octave/doc/interpreter/Status-of-Variables.html#XREFexist

other return values. 2, if the name is an absolute file name, a regular file in the Octaves path, or (after adding ".m) a function file in the Octaves path, 3 if it is a .oct name or .mex file in the Octaves path, 5 if the name is a built-in function , 7 if the name is a directory, or 103 if the name is a function not associated with the file (entered on the command line), otherwise return 0.

+2
source

All Articles