lør, 17 10 2009 kl. 13:45 +0200, skrev Eric Chassande-Mottin:
> >        b =
> >
> >            '2'
> >            ''
> 
> OK, I'm almost there but I'm blocked by a last problem
> with vectors of strings. I'm not able to create a vector b
> as above. I receive the following error:
> 
>  a=cell(2);a{1}="2";a{2}=""; b=cell(2,1); b=a{1,:}
> error: invalid assignment of comma-separated list
> 
> how can I extract a vector of strings from a cell array?

Basically, you just have to index the cell array with parenthesis
instead of curly brackets; gives you a new cell array. I've attached a
version of your code that does this. I've also changed:

  * Changed the order of the copyright and the help text (this is the
standard in Octave)

  * Simplified your switch-statements by combining "%d" and %f".

I'll run some tests of your code and compare with Matlab to see how well
things work. I'll report back later.

Thanks for doing this,
Søren
## 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 .

## -*- texinfo -*-
## @deftypefn {Function File}  {...@var{a} @var{b} ...]=}textread(@var{filename},@var{format})
## @deftypefnx {Function File} {...@var{a} @var{b} ...] =}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
##
## Currently implemented @var{prop} arguments are:
## @itemize
## @item \"headerlines\":
## @var{value} represents the number of header lines to skip.
## @end itemize
##
## @seealso{load, dlmread, fscanf}
## @end deftypefn

function varargout = textread(file, formatstr, prop, val)
 if nargchk(2,4,nargin)
   print_usage ();
 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]);
 n=length(idx);

 if (nargout!=n)
   error("textread: the number of output variables must match that of format specifiers");
 endif

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

   if isempty(this)
     continue
   endif

   this=deblank(this);

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

   while ~isempty(this)

     m=1;
     while (m <= n)

       ## read data
       data=sscanf(this,"%s",1);

       ## if no data
       if isempty(data)

         switch specif(m,:)
           case "%s"
             res{m,k}="";
           case {"%d", "%f"}
             res{m,k}=[];
         endswitch
         m++;
         continue
       endif

       ## map to format
       switch specif(m,:)
         case "%s"
           res{m,k}=char(data);
         case {"%d", "%f"}
           res{m,k}=str2num(data);
       endswitch

       ## suppress read data from buffer
       this=this(length(data)+2:end);

       m++;
     endwhile  ## m <= n
     k++;
   endwhile  ## ~isempty(this)

 endwhile ## ~feof(fid)


 ## map to output structures
 for m=1:n
   switch specif(m,:)
     case "%s"
       varargout{m} = res (m, :);
     case {"%d", "%f"}
       varargout{m} = [res{m,:}];
   endswitch
 endfor

 ## close file
 fclose(fid);
 
endfunction
------------------------------------------------------------------------------
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