help-octave
[Top][All Lists]
Advanced

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

Re: loading time data format


From: A. Danial
Subject: Re: loading time data format
Date: Sun, 11 Nov 2001 09:48:51 -0800
User-agent: Mutt/1.2.5i

On Sun, Nov 11, 2001 at 05:21:56PM +0100, Juraj Krempasky wrote:
> I'd like to load data with timestamps of the following format: "%m/%d/%Y
> %H:%M:%S" with the load command. I don't know how to instruct the load
> command to deal with this data format. Is it possible?

The fscanf function is the easiest way to read formatted strings.

Alternatively, you can try my crude implementation of matlab's textread 
function.  It is implemented in the files textread.m and str_incr.m below.

  [mo, d, y, h, mi, s] = textread('data.txt', '%d/%d/%d %d:%d:%d');

should return arrays containing months, days, years, et cetera.    -- Al

------------ textread.m
function [...] = textread(file, format, n, ...);

    # [a,b,c,...] = textread(file, format, n, ... );
    #
    # Reads data from the file using the provided format string.  The
    # first return value will contain the first column of the file,
    # the second return value gets the second column, et cetera.
    # If n is given, the format string will be reused n times.  
    # If n is omitted or negative, the entire file will be read.
    #
    # Limitations:  fails if the format string contains literal
    #               percent signs (ie, '%%'), or literal strings
    #               containing characters 's' or 'c'.

    if ((nargin == 2) || n < 0)
        n = 0;
    endif

    [fid, message] = fopen(file, "r");
    if (fid == -1)
        error(sprintf("textread('%s',...):  %s\n", file, message));
    endif

    # Step 1:  Break apart the format string into an array of formats, 
    #          one for each column.

    fmts = split(format, "%");          # fails if input file has embedded %'s
    first_fmt_len = size(fmts(1,:), 2); # the number of columns to expect
    if (sum(isspace(fmts(1,:))) == first_fmt_len) 
        # the first string is empty because format started w/%; skip it
        first_fmt_idx = 2;
    else
        # then the first format substring is not empty; need to use it
        first_fmt_idx = 1;
    endif
    nFields = size(fmts,1);


    # Step 2:  Read the contents of the file one term at a time.
    #          Place the terms into temporary octave matrices
    #          called 'aa', 'ab', et cetera.  Each pass through
    #          the format strings yields one new row in these working
    #          matrices.

    row = 1;
    while (!feof(fid))
        mat_name = "aa"; # iterate through "aa", "ab", "ac", ... "zz"
        for i = first_fmt_idx:nFields
            pfmt   = strcat("%", fmts(i,:));  # put % at start of format string
            [data] = fscanf(fid, pfmt, "C");
            if (index(pfmt, "%s")                      || 
                ((pfmt(1) == "%") && index(pfmt, "s")) || # eg, match %17s
                ((pfmt(1) == "%") && index(pfmt, "c")) )  # eg, match %17c
                # then this is a string or a group of chars
                eval(sprintf("%s(%d,1:%d) = data;", 
                             mat_name, row, size(data,2)));
            else                       # this is a scalar
                eval(sprintf("%s(%d) = data;", mat_name, row));
            endif
            mat_name = str_incr(mat_name, 2); # eg, "aa" becomes "ab"
        endfor
        ++row;

        if (n && row > n)
            break;
        endif 

    endwhile
    fclose(fid);

    # Step 3:  populate the return values with the contents of aa, ab, etc 

    mat_name = "aa";
    for i = 1:nargout
        vr_val( eval(mat_name) );
        mat_name = str_incr(mat_name, 2);
    endfor
    return

endfunction

------------ str_incr.m
function [new_name] = str_incr(old_name, nLetters);
    # [new_name] = str_incr(old_name, nLetters);
    #
    #  Increments by one letter the substring comprising the first nLetters
    #  of old_name.  The first nLetters may contain only characters in
    #  'a'.. 'z'.
    #  
    #  Examples:  str_incr("aa",   2) returns "ab"  
    #             str_incr("ab",   2) returns "ac"  
    #             str_incr("az",   2) returns "ba"  
    #             str_incr("zz",   2) returns an overflow error
    #             str_incr("abcd", 2) returns "accd"  

    letters = toascii(old_name);
    if (nLetters > size(old_name, 2))
        error("str_incr:  2nd argument too large\n");
    endif

    for i = nLetters:-1:1

        # make sure the letter is in range
        if ( (letters(i) < toascii("a")) || (letters(i) > toascii("z")) )
            err_msg = sprintf(
            "str_incr: character %d not in 'a'..'z'\n", i);
            error(err_msg);
        endif

        if (letters(i) < toascii("z"))
            ++letters(i);
            break;
        elseif (i == 1)  # bummer, ran out of characters
            error("str_incr:  name length overflow\n");
        else
            letters(i) = toascii("a");
        endif

    endfor

    new_name = setstr(letters);
    return
endfunction



-------------------------------------------------------------
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]