load command question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

load command question

Myriam Abramson

How do I load a file with a variable name?


octave> mypattern
mypattern = random4.out
octave> mypattern
mypattern = random4.out
octave> load mypattern
error: load: couldn't open input file `mypattern'
octave> load "mypattern"
error: load: couldn't open input file `mypattern'
octave>

thanks,

myriam
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: load command question

Quentin Spencer
Myriam Abramson wrote:

>How do I load a file with a variable name?
>
>
>octave> mypattern
>mypattern = random4.out
>octave> mypattern
>mypattern = random4.out
>octave> load mypattern
>error: load: couldn't open input file `mypattern'
>octave> load "mypattern"
>error: load: couldn't open input file `mypattern'
>octave>
>  
>

try:
load(mypattern)

-Quentin
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: load command question

Stéfan van der Walt
On Sun, May 07, 2006 at 07:04:43PM -0500, Quentin Spencer wrote:

> Myriam Abramson wrote:
>
> >How do I load a file with a variable name?
> >
> >
> >octave> mypattern
> >mypattern = random4.out
> >octave> mypattern
> >mypattern = random4.out
> >octave> load mypattern
> >error: load: couldn't open input file `mypattern'
> >octave> load "mypattern"
> >error: load: couldn't open input file `mypattern'
> >octave>

A useful trick is to build command lines from strings.  For example,
if you have

x = "1"
filename = "myfile"

you can do

exec(["load(", filename, x, ".dat)"])

which will execute

load(myfile1.dat)

Regards
Stéfan
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: load command question

Quentin Spencer
Stefan van der Walt wrote:

>On Sun, May 07, 2006 at 07:04:43PM -0500, Quentin Spencer wrote:
>  
>
>>Myriam Abramson wrote:
>>
>>    
>>
>>>How do I load a file with a variable name?
>>>
>>>
>>>octave> mypattern
>>>mypattern = random4.out
>>>octave> mypattern
>>>mypattern = random4.out
>>>octave> load mypattern
>>>error: load: couldn't open input file `mypattern'
>>>octave> load "mypattern"
>>>error: load: couldn't open input file `mypattern'
>>>octave>
>>>      
>>>
>
>A useful trick is to build command lines from strings.  For example,
>if you have
>
>x = "1"
>filename = "myfile"
>
>you can do
>
>exec(["load(", filename, x, ".dat)"])
>
>which will execute
>
>load(myfile1.dat)
>  
>

You can also do things like this with sprintf, which I think gives even
more flexibility:

x="1";
filename="myfile";
load(sprintf("%s%s.dat",filename,x));

or

x=1;
filename="myfile";
load(sprintf("%s%i.dat",filename,x));



Quentin
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: load command question

Bill Denney
In reply to this post by Stéfan van der Walt
On Mon, 8 May 2006, Stefan van der Walt wrote:

> x = "1"
> filename = "myfile"
> exec(["load(", filename, x, ".dat)"])
>
> which will execute
>
> load(myfile1.dat)

I think you were meaning eval instead of exec, and you can't contatenate
strings and numbers like that-- you need something like num2str.  Also, a
simpler, safer, and probably faster way to do this would be to do this:

filename = sprintf("myfile%d.dat", x);
load(filename);

It's simpler because you don't have as much indirection.  It's safer
because you don't have to worry (or at least worry as much) about what
characters are in the filename (what if you accidentally put a | in
there).  I think it's faster because I think that eval is a relatively
slow call.

Bill

--
"Only a life lived for others is worth living." -- Albert Einstein

_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: load command question

Stéfan van der Walt
On Sun, May 07, 2006 at 10:31:53PM -0400, Bill Denney wrote:

> On Mon, 8 May 2006, Stefan van der Walt wrote:
>
> >x = "1"
> >filename = "myfile"
> >exec(["load(", filename, x, ".dat)"])
> >
> >which will execute
> >
> >load(myfile1.dat)
>
> I think you were meaning eval instead of exec, and you can't contatenate

You're right -- I meant to type eval.

> strings and numbers like that-- you need something like num2str.  Also, a

Look carefully -- x and filename were both strings :)

Cheers
Stefan
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: load command question

Bill Denney
On Mon, 8 May 2006, Stefan van der Walt wrote:

> On Sun, May 07, 2006 at 10:31:53PM -0400, Bill Denney wrote:
>> On Mon, 8 May 2006, Stefan van der Walt wrote:
>>
>>> x = "1"
>>> filename = "myfile"
>>> exec(["load(", filename, x, ".dat)"])
>>>
>>> which will execute
>>>
>>> load(myfile1.dat)
>> strings and numbers like that-- you need something like num2str.  Also, a
> Look carefully -- x and filename were both strings :)

Sorry, I did miss that.

Bill

--
If you make a mistake you right it
immediately to the best of your ability.
   -- /usr/bin/fortune

_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave