help-octave
[Top][All Lists]
Advanced

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

Re: Saving variables in text and archive search


From: Matteo Bettella
Subject: Re: Saving variables in text and archive search
Date: Thu, 25 May 2000 10:26:50 +0100 (BST)


On Wed, 24 May 2000, Neil Ottenstein wrote:

> I was wondering if it was possible to save variables into an ascii text
> file in Octave in a format other than the default ascii text format which
> gives the various information about the variables and then the variables.
> What I was looking for was a text file with the values in columns.  Such as
> saving arrays a, b, c such that they appeared as such:
> 
>       a(1)    b(1)    c(1)
>       a(2)    b(2)    c(2)
>       .       .       .       
>       .       .       .
>       .       .       .
>       a(n)    b(n)    c(n)
> 
> I didn't notice any info on this in particular in the archive years I've
> looked at so far.  By the way, is there a search engine for accessing the
> archive help files or do you really need to access one at a time and search?
> 
> Thanks,
> 
> Neil
> 
> 
I can think about two ways of doing it.
Firstly you can generate a matrix containing each single
column vector [a,b,c,...] and then save it with
"save -ascii".
You get the default header with information you may
not need.

Alternatively you can open a text file, write data via
fprintf command and close it at the end. You can 
decide formats (decimal, float), the number of digits,
separators (tab, spaces, etc). You can put comment 
strings as well!

Have a look at the example attached.

------------------------------------------------------------
FILE save_example.m
------------------------------------------------------------

colum_vector = (0:9)';

% Now 3 column vectors are generated:
% (a,b,c will have the same number of rows.)

a = colum_vector * 10^(-1);
b = colum_vector * 10^(0);
c = colum_vector * 10^(1);

% and put in the same matrix, in columnform:

the_array = [a,b,c];

save -ascii data.txt the_array

% You get a text file containing 
% a(1) b(1) c(1)
% ...
% a(n) b(n) c(n)

% But if you want to save it in YOUR OWN format:
% (a,b,c need to have the same length)

fname = 'data2.txt';
[fp,errmsg] = fopen(fname,'w'); 
if fp==-1,
        disp(['Opening file ' fname]);
        error(errmsg);
end

for i=1:length(a), 
        fprintf(fp,'%.2f %.4f %e\n', a(i),b(i),c(i));
end % for i

fclose(fp);

------------------------------------------------------------    
------------------------------------------------------------
file: data.txt
------------------------------------------------------------
# Created by Octave 2.0.14, Thu May 25 08:59:06 2000 <address@hidden>
# name: the_array
# type: matrix
# rows: 10
# columns: 3
 0 0 0
 0.1 1 10
 0.2 2 20
 0.3 3 30
 0.4 4 40
 0.5 5 50
 0.6 6 60
 0.7 7 70
 0.8 8 80
 0.9 9 90
------------------------------------------------------------    
------------------------------------------------------------
file: data2.txt
------------------------------------------------------------
0.00 0.0000 0.000000e+00
0.10 1.0000 1.000000e+01
0.20 2.0000 2.000000e+01
0.30 3.0000 3.000000e+01
0.40 4.0000 4.000000e+01
0.50 5.0000 5.000000e+01
0.60 6.0000 6.000000e+01
0.70 7.0000 7.000000e+01
0.80 8.0000 8.000000e+01
0.90 9.0000 9.000000e+01
------------------------------------------------------------




        -------------------------------------
        Matteo Bettella

        School of Mechanical Engineering
        Automotive Engineering Group
        Cranfield University
        Cranfield - Beds
        MK43 0AL

        Tel.: (00)44-(0)1234-754652
        E-mail: address@hidden
        -------------------------------------




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

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



reply via email to

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