help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regarding opening a file in octave script


From: Juan Pablo Carbajal
Subject: Re: Regarding opening a file in octave script
Date: Thu, 24 May 2018 00:09:57 +0200

On Wed, May 23, 2018 at 10:51 PM, NAGARAJAN <address@hidden> wrote:
> Hi all,
>
>    I am very new to octave and trying to edit an octave script. I have to
> pass the folder name as a command line argument and to open a file inside
> the octave script
>
>    For example:
>
>       data=load("./tmp.in")  #This command will open the file tmp.in in the
> current folder. Instead of using current folder, I have to give the folder
> name each time
>
>
>     So, I did like this
> octave < run.oct folder_name
>
>     Then I received the value in the octave script like this
> folder= argv(){1};
>
>     Then modified the the file loading line like this
> data=load("folder/tmp.in");
>
>     But, it is not working
>
>    Please let me know how to solve this problem.
>
> Thank you
>
Hi, welcome.

There are many ways to solve your problem. I will give you one of them.
But first: scripts and functions should be saved in files with
extension .m (m-files; read the manual [1])

If you want to run your script from the command line (as implied from
your example) you could do the following:

octave --eval 'filename = "tmp.ini"; my_script'

Your script (which I renamed and saved in a file my_script.m) can
assume there will be a variable named filename define din the
workspace and do

if ~exists('filename', var'')
  error ('The variable filename should be defined');
else
  data = load (filename);
endif

if you are automatizing at the command line level, you could create a
shell script with contents

// saved in e.g.  run_script.sh
octave --eval 'filename = $1; my_script'

and call it

$ ./run_script.sh tmp.ini


I hope this goes in the direction of solving your problem

Regards.

[1] 
https://octave.org/doc/interpreter/Functions-and-Scripts.html#Functions-and-Scripts



reply via email to

[Prev in Thread] Current Thread [Next in Thread]