Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 995
smilingbuddha Avatar asked May 14 '12 14:05

smilingbuddha


People also ask

How do you call a file in Octave?

Simply type the filename without extension . Type run("filename. m") in the command line of Octave .

How do you define and call a function in Octave?

Defining Functions. A valid function name is like a valid variable name: a sequence of letters, digits and underscores, not starting with a digit. Functions share the same pool of names as variables. The function body consists of Octave statements.


1 Answers

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.

like image 131
Sevenless Avatar answered Oct 13 '22 06:10

Sevenless