help-octave
[Top][All Lists]
Advanced

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

Re: textscan preserving all whitespace


From: Ben Abbott
Subject: Re: textscan preserving all whitespace
Date: Fri, 12 Sep 2014 08:20:33 -0400

On Sep 11, 2014, at 6:08 PM, rmales <address@hidden> wrote:

> I apologize if this has been previously answered, I did search, it is clear
> that there are many questions about textscan, but I did not see this
> particular one (may be my ignorance).
> 
> I am using the experimental version of Octave for Windows 8 (v 3.8.2) with
> the GUI, it works very well.
> 
> I have inherited some Matlab scripts that are used to create and parse
> various Ascii files, and have been successful in converting them quite
> easily to Octave.  However, I have one script that reads an entire ascii
> file into an array and then has lots of parsing of that array that depends
> upon preserving the whitespace, i.e. the exact number of spaces between
> fields.   The input is processed through:
> 
> fid = fopen('ODOC');
> tot = textscan(fid,'%s','delimiter','\n');
> 
> An example of the input that needs to be handled is: 
>      3600.0     8.0000     2.1500     0.0000     0.2500     0.0000
>      7200.0     8.0000     2.2500     0.0000     0.6830     0.0000
>    10800.0     8.0000     2.3500     0.0000     0.9330     0.0000
>    14400.0     8.0000     2.4500     0.0000     0.9330     0.0000
>    18000.0     8.0000     2.5500     0.0000     0.6830     0.0000
> 
> When textscan processes this, the whitespace is crunched out so that there
> is only a single whitespace character between fields.   This causes problems
> for the rest of the script which is expecting fields to be in pre-defined
> columns.  At present, I cannot change the format of the input Ascii file, I
> am stuck with it.
> 
> Any suggestions on how to modfy the textscan command or use an alternative
> method of reading the file that will preserve the spaces will be much
> appreciated.   I am aware that Matlab textscan is not identical to Octave
> textscan, was not expecting that to be the case.
> 
> Thank you all for this truly superior piece of software.
> 
> R. Males
> Cincinnati, Ohio
> USA

You just want to read the lines into a cell array of strings?

        lines = strsplit ((fileread ('ODOC')), "\n");

To convert to doubles ...

        data = str2num (strcat('[',strcat(lines(1:end),';'){:},']'));

Or if you only need the numeric data ...

        data = load ('ODOC')

Ben






reply via email to

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