|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
Writing a commented output to fileHi,
I'm writing a script which eventually needs to write a matrix to a text file, and prepend some comments to it (e.g. title line and the starting conditions that led to the output). For example, if the matrix is a = 1 2 3 4 5 6 7 8 9 I want the output file to look like that: % Some comment 1 % Some comment 2 1 2 3 4 5 6 7 8 9 Now, since the matrices can get quite big I don't want to use printf() for every line since it may take a lot of time. Ideally I'd like to use save -ascii to save the matrix and printf() for the comments. The problem is this - if I'm using save to write the matrix first I can't prepend the comments since there is no way to open a file for prepending but only for appending. If I write the comments first and then call save -ascii it just overwrites the contents of the output file and the file will only contain the matrix without the comments. Any ideas on how I can get both without just using printf() for everything? Thanks, Alex |
|
|
Re: Writing a commented output to fileThis might seem like a bit of a hack, but if you're on linux you could do something like
save tmpfile variable -ascii system('echo \'comment line 1\' > filename'); system('echo \'comment line 2\' >> filename'); system('cat tmpfile >> filename'); The syntax maybe off, but hopefully I've conveyed the idea. I assume there is something similar if you're using windows. Hope this helps. James On Tue, May 5, 2009 at 7:55 AM, AlexG1 <alxgel@...> wrote:
_______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: Writing a commented output to fileOn May 5, 2009, at 7:55 AM, AlexG1 wrote:
> The problem is this - if I'm using save to write the matrix first I > can't > prepend the comments since there is no way to open a file for > prepending but > only for appending. > If I write the comments first and then call save -ascii it just > overwrites > the contents of the output file and the file will only contain the > matrix > without the comments. > > Any ideas on how I can get both without just using printf() for > everything? comment = sprintf(...); save -text somefile comment data Cheers, Rob -- Rob Mahurin Department of Physics and Astronomy University of Tennessee 865 207 2594 Knoxville, TN 37996 rob@... _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Writing a commented output to fileOn 5-May-2009, AlexG1 wrote:
| Now, since the matrices can get quite big I don't want to use printf() for | every line since it may take a lot of time. Ideally I'd like to use save | -ascii to save the matrix and printf() for the comments. | | The problem is this - if I'm using save to write the matrix first I can't | prepend the comments since there is no way to open a file for prepending but | only for appending. | If I write the comments first and then call save -ascii it just overwrites | the contents of the output file and the file will only contain the matrix | without the comments. | | Any ideas on how I can get both without just using printf() for everything? Here's one more way: fid = fopen ('foo.dat', 'w'); fprintf (fid, '%% Some comment 1\n%% Some comment 2\n'); nc = size (x, 2); fmt = sprintf ('%s\n', repmat ('%f ', [1, nc])); fprintf (fid, fmt, x'); fclose (fid); I'm not sure whether it matters to you, but this method also has the advantage of being compatible with the other leading brand. jwe _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: Writing a commented output to fileThanks, works great. There's something I didn't understand though - can you please explain why the transpose on the fprintf() call is needed? |
|
|
Re: Writing a commented output to fileOn Wed, May 6, 2009 at 12:38 PM, AlexG1 <alxgel@...> wrote:
> > > > John W. Eaton-3 wrote: >> >> >> Here's one more way: >> >> fid = fopen ('foo.dat', 'w'); >> fprintf (fid, '%% Some comment 1\n%% Some comment 2\n'); >> nc = size (x, 2); >> fmt = sprintf ('%s\n', repmat ('%f ', [1, nc])); >> fprintf (fid, fmt, x'); >> >> >> > > Thanks, works great. > There's something I didn't understand though - can you please explain why > the transpose on the fprintf() call is needed? because fprintf always traverses the data in the memory storage order, i.e. columns first (as if "x(:)" was used). regards -- RNDr. Jaroslav Hajek computing expert & GNU Octave developer Aeronautical Research and Test Institute (VZLU) Prague, Czech Republic url: www.highegg.matfyz.cz _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
| Free embeddable forum powered by Nabble | Forum Help |