help-octave
[Top][All Lists]
Advanced

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

Re: problem reading file with time-stamps


From: Ben Abbott
Subject: Re: problem reading file with time-stamps
Date: Tue, 13 Dec 2011 18:14:27 -0500

On Dec 13, 2011, at 3:22 PM, BVBA NuKey Music wrote:

> 2011/12/13 Ben Abbott <address@hidden>
> 
>> On Dec 13, 2011, at 9:28 AM, BVBA NuKey Music wrote:
>> 
>> > 2011/12/13 Ben Abbott <address@hidden>
>> >
>> >> On Dec 13, 2011, at 8:24 AM, BVBA NuKey Music wrote:
>> >>
>> >> > 2011/12/13 Ben Abbott <address@hidden>
>> >> >>
>> >> >> On Dec 13, 2011, at 7:39 AM, BVBA NuKey Music wrote:
>> >> >>
>> >> >> > 2011/12/13 Joanna Cheng <address@hidden>
>> >> >> >> On Tue, Dec 13, 2011 at 7:51 PM, BVBA NuKey Music <address@hidden> 
>> >> >> >> wrote:
>> >> >> >> > I'm trying to read a file with four columns which has timestamps 
>> >> >> >> > in the
>> >> >> >> > first column:
>> >> >> >> >
>> >> >> >> > The data is available in the file in the following format:
>> >> >> >> > Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00
>> >> >> >> > Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00
>> >> >> >> > Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00
>> >> >> >> >
>> >> >> >> > first field is a timestamp then a space followed by a temperature 
>> >> >> >> > then a
>> >> >> >> > space ...
>> >> >> >> >
>> >> >> >> > As you can see here below, octave doesn't accept the file because 
>> >> >> >> > of the
>> >> >> >> > timestamps,
>> >> >> >> > can anyone here help me solve this problem?
>> >> >> >> >
>> >> >> >> > octave:1> y=load('x7r0.7vac0vdc.dat');
>> >> >> >> > error: load: failed to read matrix from file `x7r0.7vac0vdc.dat'
>> >> >> >> > error: evaluating assignment expression near line 1, column 2
>> >> >> >>
>> >> >> >>
>> >> >> >> I actually wrote a bit of code to do this with csv files. See here:
>> >> >> >> http://www.octave.org/wiki/index.php?title=Tips_and_tricks#Load_Comma_Separated_Values_.28.2A.csv.29_files
>> >> >> >>
>> >> >> >> Sample output as below:
>> >> >> >>
>> >> >> >> octave:19> A=textread("timestamps.txt", "%d", "delimiter", " ");
>> >> >> >> octave:20> B=textread("timestamps.txt", "%s", "delimiter", " ");
>> >> >> >> octave:21> inds = isnan(A);
>> >> >> >> octave:22> B(!inds) = num2cell(A(!inds));
>> >> >> >> octave:23> B
>> >> >> >> B =
>> >> >> >> {
>> >> >> >>  [1,1] = Dec-13-09:46:45
>> >> >> >>  [2,1] =  21.400
>> >> >> >>  [3,1] =  0.47644
>> >> >> >>  [4,1] = 8.1355e-006
>> >> >> >>  [5,1] =  1.5537
>> >> >> >>  [6,1] = Dec-13-09:47:12
>> >> >> >>  [7,1] =  21.400
>> >> >> >>  [8,1] =  0.47644
>> >> >> >>  [9,1] = 8.1358e-006
>> >> >> >>  [10,1] =  1.5537
>> >> >> >>  [11,1] = Dec-13-09:47:39
>> >> >> >>  [12,1] =  21.400
>> >> >> >>  [13,1] =  0.47643
>> >> >> >>  [14,1] = 8.1363e-006
>> >> >> >>  [15,1] =  1.5539
>> >> >> >> }
>> >> >> >> octave:24> class(B)
>> >> >> >> ans = cell
>> >> >> >> octave:25> class(B{1})
>> >> >> >> ans = char
>> >> >> >> octave:26> class(B{2})
>> >> >> >> ans = double
>> >> >> >>
>> >> >> >> Hope that helps,
>> >> >> >> Joanna
>> >> >> >
>> >> >> > Thanks for replying but if I try out your code I get the following 
>> >> >> > errors:
>> >> >> > octave:3> A=textread("timestamps.txt", "%d", "delimiter", " ")
>> >> >> > error: `textread' undefined near line 3 column 3
>> >> >> > error: evaluating assignment expression near line 3, column 2
>> >> >> >
>> >> >> > and
>> >> >> >
>> >> >> > octave:3> B=textread("timestamps.txt", "%s", "delimiter", " ");
>> >> >> > error: `textread' undefined near line 3 column 3
>> >> >> > error: evaluating assignment expression near line 3, column 2
>> >> >> >
>> >> >> > Any idea what is going wrong?
>> >> >> >
>> >> >> > thanks in advance
>> >> >> > nukey
>> >> >>
>> >> >> (please reply below so that others arriving late can more easilly 
>> >> >> follow along)
>> >> >>
>> >> >> I suspect you are using Octave 3.2 ? correct?
>> >> >>
>> >> >> Joanna is likely using Octave 3.4.x or the developers sources (3.6.x).
>> >> >>
>> >> >> If you are unable to upgrade your version of Octave, can you install 
>> >> >> the io package? You'll want to be sure that the version is < 1.0.13. 
>> >> >> The versions more recent that this do not include the textread() 
>> >> >> function (it was moved to Octave's core).
>> >> >>
>> >> >> Ben
>> >> >
>> >> > OK, I installed octave-io but I'm still facing problems:
>> >> > octave:5> A=textread("timestamps.txt", "%d", "delimiter", " ");
>> >> > error: Unknown property delimiter.
>> >> > error: Invalid argument specified
>> >> >
>> >> > So I removed the word "delimiter" which gave me:
>> >> > octave:7> A=textread("timestamps.txt", "%d", " ")
>> >> > A =
>> >> >
>> >> >    3.6074e-313
>> >> >    3.6074e-313
>> >> >    3.6074e-313
>> >> >
>> >> > octave:8> A=textread("timestamps.txt", "%s", " ")
>> >> > A =
>> >> >
>> >> > {
>> >> >   [1,1] = Dec-13-09:46:45
>> >> >   [2,1] = Dec-13-09:47:12
>> >> >   [3,1] = Dec-13-09:47:39
>> >> > }
>> >> >
>> >> >
>> >> > But how can I get my data in a matrix?
>> >> >
>> >> > thanks in advance
>> >> > nukey
>> >> >
>> >>
>> >> *** please reply below so that those arriving late can follow along ***
>> >>
>> >> This version of textread() may not support the "delimiter" option. 
>> >> However, whitespace is treated as a delimiter by default, so you don't 
>> >> need to specify it.
>> >>
>> >> The data as a string followed by 4 floats (correct?). So the commands 
>> >> below should work.
>> >>
>> >>        fmt = "%s %f %f %f %f";
>> >>        [A, B, C, D, E] = textread ("timestamp.txt", fmt);
>> >>
>> >> Ben
>> >
>> > Wow, that works great, is there a way to achieve the following too?
>> > y=[A B C D E]
>> > I noticed that y=[B C D E] works but I can't add the first column, 
>> > probably because it is of a different data-type
>> >
>> 
>> *** please reply below so that those arriving late can follow along ***
>> 
>> Since you are mixing character data with doubles, you'll need to use a cell 
>> array for that.
>> 
>>        y = {A, B, C, D, E};
>> 
>> Ben
> 
> Thank you Ben, this works to a certain level 
> but when I write back 'y' (normally after doing some manipulations on  the 
> data first of course) like this:
> save('test.dat','y');
> I don't get my space separated values format back that way in the file 
> test.dat
> I just don't understand why it is not possible to read in my blank separated 
> datafile in matrix-form, do some manipulations on the columns and then write 
> it back in the original format as another file.
> 
> thanks in advance
> nukey

The save() command doesn't work like that. It is intended to save data of 
different types. When saved, the type of data, and variable name is included.

The reason you can't read the data in matrix form is because a matrix represent 
numeric data, not character data (your first column is character).

To preserve the format of your file, you can ...

        fid = fopen ("data.dat");
        for n = 1:numel(A)
                fprintf (fid, "%s %g %g %g %g\n", A{n}, B(n), C(n), D(n), E(n))
        endfor
        fclose (fid)

Ben




reply via email to

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