what about that?

============================================================================

function res = textread(file, formatstr, prop, val)

## -*- texinfo -*-
## @deftypefn {Function File}  {...@var{r}]=}
textread(@var{filename},@var{format})
## @deftypefnx {Function File} {...@var{r} =}
textread(@var{filename},@var{format},@var{prop},@var{value})
## Read data from a text file.
## The string @var{format} describes the different columns of the text file and
## It may continue the following specifiers:
## @table @code
## @item %s
## for a string,
##
## @item %d,%f
## for a double, floating-point or integer number and
##
## @item %*
## to ignore a column.
## @end table
##
## For example, the textfile containing
##
## @example
## @group
## Bunny Bugs   5.5
## Duck Daffy  -7.5e-5
## Penguin Tux   6
## @end group
## @end example
##
## can be read using
##
## @example
## @code{[a,b,c] = textread("test.txt", "%s %s %f").}
## @end example
##
## @end deftypefn
## @seealso{load, dlmread, fscanf}

## Currently implemented @var{prop} arguments are:
## @itemize
## @item \"headerlines\":
## @var{value} represents the number of header lines to skip.
## @end itemize

## Copyright (C) 2009 Eric Chassande-Mottin, CNRS (France)

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see .

  if nargchk(2,4,nargin)
    usage("textread.m: r = textread( filename, format, [prop, val] )");
    end;

  comment_flag=false;

  if nargin>2
    switch prop
      case "commentstyle"
        comment_flag=true;
        switch val
            case "c"
              comment_specif="/*";
            case "c++"
              comment_specif="//";
            case "shell"
              comment_specif="#";
            case "matlab"
              comment_specif="%";
            otherwise
              error("textread: unknown comment style %s",val);
          endswitch
    endswitch
  endif

  # open file
  fid = fopen(file,"r");

  # parse format string
  idx=strfind(formatstr,"%")';
  specif=formatstr([idx,idx+1]);

  # create output structure
  n=length(idx);
  res=cell(n,1);

  # read line
  k = 1; m=1;
  while ~feof(fid)
    this = fgetl(fid);

    ## ignore line if it is a comment
    if comment_flag
      buffer=strjust(this,"left");
      if strcmp(buffer(1:length(comment_specif)),comment_specif)
        continue
      endif
    endif

    ## if one specifier only
    if (n==1)
      while ~isempty(data=sscanf(this,"%s",1))
        data
        switch specif
          case "%s"
            res{m}=char(data);
          case "%d"
            res{m}=str2num(data);
          case "%f"
            res{m}=str2num(data);
        endswitch
        this=this(length(data)+2:end);
        m++;
      endwhile
    else
    ## if several specifiers
      m=1;
      while (m<=n)
        data=sscanf(this,"%s",1)
        switch specif(m,:)
          case "%s"
            res{m,k}=char(data);
          case "%d"
            res{m,k}=str2num(data);
          case "%f"
            res{m,k}=str2num(data);
        endswitch
        this=this(length(data)+2:end);
        m++;
      endwhile
    endif

    k++;
  endwhile

  ## close file
  fclose(fid);

endfunction


============================================================================

On Tue, Oct 13, 2009 at 9:07 AM, Søren Hauberg <so...@hauberg.org> wrote:
> Hi,
>
> Thanks for taking up this challenge :-)
>
> man, 12 10 2009 kl. 13:08 +0200, skrev Eric Chassande-Mottin:
>> here is a possible implementation of textread.m:
>
> First, could you include a license statement in the code you post? I
> know it seems silly when the code is just being drafted, but we really
> need to get the license straight from the start.
>
> About the function, then I tried loading the file you previously sent:
>
>        # comment
>        # comment
>        1 2 3
>
> Using your code, I get
>
>        a = textread ("file.txt", "%s")
>        warning: setstr is obsolete and will be removed from a future
>        version of Octave; please use char instead
>        a =
>
>        {
>          [1,1] = #
>          [1,2] = #
>          [1,3] = 1
>        }
>
> whereas Matlab gives me
>
>        a =
>
>            '#'
>            'comment'
>            '#'
>            'comment'
>            '1'
>            '2'
>            '3'
>
> So, results aren't quite right yet. I can't figure out exactly what
> Matlab does as I find the documentation incomprehensible :-(
>
> Søren
>
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to