如何检查变量是否在 Octave 中定义?

发布于 2024-12-21 10:57:04 字数 54 浏览 2 评论 0 原文

当编写加载数据的脚本时,每次等待它加载都是浪费时间。

如何检查变量是否已定义?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身软味 2024-12-28 10:57:04

您可以使用 Octave 中的 exist 函数来完成这项工作。它可用于检查给定名称作为变量、内置函数、文件或目录是否存在。在您的情况下,要检查变量是否存在,您可以使用类似以下内容:

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

您可以参考以下链接以获取详细信息:

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:

叶落知秋 2024-12-28 10:57:04

还需要将变量名称放在引号中,

exist("varname", "var")

Need to put the variable name in quotes too,

exist("varname", "var")

从来不烧饼 2024-12-28 10:57:04
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

注意 itemcount 用双引号引起来。

通过更改 == 之后的值,您可以

从 / 更多信息中检查函数名称、文件名、目录、路径等:
https://www.gnu.org/software /octave/doc/interpreter/Status-of-Variables.html#XREFexist

其他返回值..
2 如果名称是绝对文件名、Octave 路径中的普通文件或(附加“.m”后)Octave 路径中的函数文件,3 如果名称是 Octave 路径中的“.oct”或“.mex”文件Octave 的路径,如果名称是内置函数,则为 5;如果名称是目录,则为 7;如果名称是与文件不关联的函数(在命令行中输入),则为 103。否则,返回 0。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文