gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:20:01 25 4
gpt4 key购买 nike

在可执行的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com