help-octave
[Top][All Lists]
Advanced

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

Re: read file, given a non-octave header


From: the_verge
Subject: Re: read file, given a non-octave header
Date: Sat, 27 Jan 2007 10:26:40 -0800 (PST)

Thomas,

Great!  That works well.  Now, I'm having another problem, that's somewhat
related:  One of my columns of data is a number with commas for thousands
separators.  How do I convert a string like: '1,200' to an integer, or
float: 1200.00

str2double('1,200')  comes close, but it reads the comma as a column
separator, so that I get:

octave:6>str2double('1,200')

ans = 
1.00 200.00

I suppose I could write a function that treats the first column as thousands
and the second column has hundreds and adds the numbers together.  Is there
an easier and more general way to do it (i.e. what if some number strings
were in the billions with lots of commas)?  Surely there must be some way to
deal with commas as thousands separators in Octave.  Any ideas?

Thanks again for the help.
Vergil





Thomas Treichl wrote:
> 
> the_verge schrieb:
>> Thomas,
>> 
>> Thanks for addressing my questions.  I'm really close to making it read
>> properly, but I'm having a small problem:  The first row in my cell array
>> is
>> always the 3rd line of the file, regardless of whether it is a comment
>> line
>> or a line of data.  For some reason Octave doesn't seem to be ignoring
>> the
>> comments.  The reason it begins reading on the 3rd line is because of the
>> fgetl(fid); fgetl(fid); that you put in there to ignore the descriptor
>> lines.  Preferably, it would skip all 35 or so lines of comments, and
>> then
>> skip the two data description lines, and then begin reading from the
>> first
>> line of data.
>> 
>> Any ideas on how to make it skip comment lines (preceded by a #)?
>> 
>> Thanks,
>> Vergil
>> 
>> P.S. For reference, when I called the function that you wrote for me,
>> here
>> was my resulting cell array (you can see the description lines in there):
> ...
> 
> Try this
> 
>     function vret = parsedata (filename)
>       [vfid, vmsg] = fopen (filename, "r");
>       if (isempty (vmsg) == false), error (vmsg); endif
> 
>       vcnt = 1;             # Initialize counter for while loop
>       vstr = fgetl (vfid);  # Get first line of data file
>       while (vstr != -1)
>         if (vstr(1) != "#") # Ignore every that starts with '#'
>           [vret{vcnt,1}, vret{vcnt,2}, vret{vcnt,3}, vret{vcnt,4}, \
>            vret{vcnt,5}, vret{vcnt,6}, vret{vcnt,7}] = \
>           sscanf (vstr, "%s\t%s\t%s\t%s\t%s\t%s\t%s", "C");
>           vcnt = vcnt + 1;
>         endif
>         vstr = fgetl (vfid);
>       endwhile
> 
>       fclose (vfid);
>     endfunction
> 
> and copy the parsed data into a new variable if you don't like the two 
> describtion lines:
> 
>    newdata = olddata{3:end,:}
> 
> Thomas
> 
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
> 
> 

-- 
View this message in context: 
http://www.nabble.com/read-file%2C-given-a-non-octave-header-tf3126856.html#a8668682
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

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