file - 将文件名作为命令行参数传递给 GNU Octave 脚本

标签 file matlab octave

在可执行的 Octave 脚本中,我想传递一个包含矩阵的文件的名称,并让 gnu Octave 将该文件信息作为矩阵加载。我该怎么做?

脚本应该是这样的

#! /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

传递的文件将是一个矩阵,其中包含一个任意大小的矩阵,比如 2x3

1 2 3
5 7 8

在命令行中,脚本应作为 ./myscript mymatrixfile 运行 其中 mymatrixfile 包含矩阵。

这是我尝试用 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]$  

其中 generators.xyz 是包含我需要的矩阵的文件

最佳答案

这应该有效:

#!/usr/bin/octave -qf

arg_list = argv ();
filename = arg_list{1};
load("-ascii",filename);

当您编写 load filename 行时,您指示加载函数加载文件名“filename”。也就是说,你做了等同于load('filename');的事情。

在 MATLAB 和 Octave 中,函数“foo”后跟一个空格,然后是单词“bar”,表示 bar 将作为字符串提交给 foo。即使 bar 是您工作区中定义的变量也是如此。

关于file - 将文件名作为命令行参数传递给 GNU Octave 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585581/

相关文章:

c - 从 PGM 文件 C 读取矩阵时出现问题

file - Selenium IDE - 用于测试的本地(非网络托管)文件访问?

android - 同时读取和写入文件

image - 清理二值图像的最佳方法

Matlab : String vector - character subtraction

java - 打开的文件太多错误

matlab - 使用相机校准估计到点的距离

matlab - 如何实现 3d 重建算法

matlab - 元胞数组中所有元素的长度向量?

machine-learning - 以 Octave 语法绘制