简体   繁体   中英

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. 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

1 2 3
5 7 8

At the command line the script should be run as ./myscript mymatrixfile where mymatrixfile contains the matrix.

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

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". That is to say, you did the thing that is equivalent to 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. This is true even if bar is a defined variable in your workspace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM