help-octave
[Top][All Lists]
Advanced

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

Re: sprintf to make filenames


From: Mike Miller
Subject: Re: sprintf to make filenames
Date: Mon, 11 Mar 2002 17:11:47 -0600 (CST)

My question:

> I want to be able to use the usual save command of this form:
>
> save -ascii file_0001.dat matrixdata
>
> but I'd like for the filename to be determined by a variable in this
> sort of way:
>
> i=1;
> save -ascii sprintf("file_%04d.dat",i) matrixdata
>
> That doesn't work because Octave doesn't seem to interpret the sprintf
> command to produce the string for the filename.  How can I get this to
> work?


Below are all the answers I received.  They were extremely helpful.  I
would say that this solution will work best for me...

save("-ascii",sprintf("file_%04d.dat",i),"matrixdata")

...but the other stuff also helped a lot.  The help-octave list is still
going strong!  (I was last on it 2 years ago.)  Thanks very much.

Mike


--------------------------------------------------------------------------


Use eval() and build the complete command string inside it. There are dozens
of examples in the list archives.


--------------------------------------------------------------------------


either
        eval(sprintf("save -ascii file_%04d.dat matrixdata", i))
or
        save("-ascii",sprintf("file_%04d.dat",i),"matrixdata")


--------------------------------------------------------------------------


Try :

  save ("-ascii",,sprintf("file_%04d.dat",i), "matrixdata")

Octave commands usually can be called as functions too.


--------------------------------------------------------------------------


Use the "eval() function", Mike,

eval(['save -ascii ', sprintf("file_%04d.dat",i),' matrixdata']);


--------------------------------------------------------------------------


That does work.  Here is an example:  "runNo" and "data_to_save"  are
defined by your code.

fnam_t= sprintf("%s.type",runNo);

str = sprintf("save %s  data_to_save",fnam_t);

eval(str);

Hope that helps.


--------------------------------------------------------------------------


You can also use the functional version of save:

a=rand(1,5);
fname="foo.mat"
save("-ascii",fname,"a");

saves the variable "a" to the file "foo.mat" as ascii.


--------------------------------------------------------------------------


Just to state what may be obvious:
I didn't use sprintf in the example. You can use sprintf to set
the variable "fname" to whatever you want.



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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