[英]Pass a file name as a command line argument to GNU Octave script
In an executable Octave script, I want to pass the name of a file containing a matrix and make gnu octave load that file information as a matrix. 在可执行的Octave脚本中,我想传递包含矩阵的文件的名称,并使gnu octave将该文件信息作为矩阵加载。 How do I do that?
我怎么做?
Here is what the script should look like 这是脚本应该是什么样子
#! /usr/bin/octave -qf
arg_list = argv()
filename = argv{1} % Name of the file containing the matrix you want to load
load -ascii filename % Load the information
The file passed will be a matrix containing a matrix of arbitrary size say 2x3 传递的文件将是一个矩阵,其中包含任意大小的矩阵,例如2x3
1 2 3
5 7 8
At the command line the script should be run as ./myscript mymatrixfile
where mymatrixfile
contains the matrix. 在命令行,脚本应该以
./myscript mymatrixfile
运行,其中mymatrixfile
包含矩阵。
This is what I get when I try to execute the script just written above with octave 这是我尝试执行上面用八度音程编写的脚本时得到的
[Desktop/SCVT]$ ./octavetinker.m generators.xyz (05-14 10:41)
arg_list =
{
[1,1] = generators.xyz
}
filename = generators.xyz
error: load: unable to find file filename
error: called from:
error: ./octavetinker.m at line 7, column 1
[Desktop/SCVT]$
Where generators.xyz
is the file containing the matrix I need generators.xyz
是包含我需要的矩阵的文件
This should work: 这应该工作:
#!/usr/bin/octave -qf
arg_list = argv ();
filename = arg_list{1};
load("-ascii",filename);
when you wrote the line load filename
you indicated to the load function to load the file name "filename". 当您编写行
load filename
您指示加载函数加载文件名“filename”。 That is to say, you did the thing that is equivalent to load('filename');
也就是说,你做了相当于
load('filename');
的事情load('filename');
. 。
In both MATLAB and Octave, a function "foo" followed by a space then the word "bar" indicates that bar is to be submitted as a string to foo. 在MATLAB和Octave中,函数“foo”后跟一个空格,然后单词“bar”表示bar将作为字符串提交给foo。 This is true even if bar is a defined variable in your workspace.
即使bar是工作空间中的已定义变量,也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.