How to check if a variable is defined in Octave?

て烟熏妆下的殇ゞ 提交于 2019-12-23 08:56:52

问题


When writing a script that loads data, it's a waste of time to wait for it to load each time.

How to check to see if the variable is defined?


回答1:


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

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

You may refer the following links for detailed information:

  • Built-in Function: exist (name, type)
  • Status of Variables



回答2:


Need to put the variable name in quotes too,

exist("varname", "var")




回答3:


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 itemcount is in double quotes.

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

from / more info 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, an ordinary file in Octave’s path, or (after appending ‘.m’) a function file in Octave’s path, 3 if the name is a ‘.oct’ or ‘.mex’ file in Octave’s 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 a file (entered on the command line). Otherwise, return 0.



来源:https://stackoverflow.com/questions/8542628/how-to-check-if-a-variable-is-defined-in-octave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!