Regarding opening a file in octave script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Regarding opening a file in octave script

NAGARAJAN
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


Reply | Threaded
Open this post in threaded view
|

Re: Regarding opening a file in octave script

Juan Pablo Carbajal-2
On Wed, May 23, 2018 at 10:51 PM, NAGARAJAN <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: Regarding opening a file in octave script

NAGARAJAN
Hi,

  Thank you for the details. But the issue is little different

  I have to load many files and the file names are same for all the runs. but the folder name will change in each run

  Thats why I need to provide only "the folder name" as command line argument like this

  octave < run.oct folder_name

 but I am not sure how to use the folder name inside the script

Any idea?

Thank you

 

On Wed, May 23, 2018 at 5:09 PM, Juan Pablo Carbajal <[hidden email]> wrote:
On Wed, May 23, 2018 at 10:51 PM, NAGARAJAN <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: Regarding opening a file in octave script

Juan Pablo Carbajal-2
Use the same ideas I gave you before...specially the one regarding the
name of your scriptfile.
To form the path to a file you can use the fullfile function