On 3/24/07, sen1 at math.msu.edu <sen1 at math.msu.edu> wrote:
> Is there a way to save a matrix, A, in a file with some strings added
> at the beginning and end?
write_data doesn't have any ability to annotate its output.
So it has to be handled by bundling some other stuff around the edges.
> I would like to save it as
>
> A=[1 1 0
> 0 1 1
> 1 1 1]
>
> so that it can be loaded into octave.
If the goal is only to get the data into Octave, Octave's load
function can read a file which is just a list of numbers
(i.e. the same format written by write_data).
That said, maybe this is useful:
write_octave_matrix (a, filename) ::=
buildq ([a, filename],
block ([file_output_append : true],
with_stdout (filename, print ('a, "= [")),
write_data (a, filename),
with_stdout (filename, print ("]\;"))));
foo : matrix ([1, 2, 3], [4, 5, 6], [7, 8, 9]);
macroexpand (write_octave_matrix (foo, "/tmp/foo.data"));
=> block([file_output_append : true],
with_stdout(/tmp/foo.data, print('foo, = [)),
write_data(foo, /tmp/foo.data),
with_stdout(/tmp/foo.data, print(];)))
load (numericalio);
write_octave_matrix (foo, "/tmp/my.data");
bar : foo^^2;
write_octave_matrix (bar, "/tmp/my.data");
printfile ("/tmp/my.data");
=>
foo = [
1 2 3
4 5 6
7 8 9
];
bar = [
30 36 42
66 81 96
102 126 150
];
Octave 2.1.50 seems to think this output is OK.
I have considered extending write_data to be able to write to
a stream as well as a filename. That would not change any
existing behavior and it would give greater flexibility in cases
like this, in which you want to combine output from two or
more functions. Something to think about.
HTH
Robert