help-octave
[Top][All Lists]
Advanced

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

Re: textscan preserving all whitespace


From: Philip Nienhuis
Subject: Re: textscan preserving all whitespace
Date: Fri, 12 Sep 2014 02:03:42 -0700 (PDT)

rmales 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.

Thanks for your kind words about Octave :-)

(Hopefully I understand you properly.)
AFAIK Matlab's textscan behaves exactly the same. The "fix", or rather: the
trick, is easy and is borrowed from the Matlab scripts we use at work (where
we hit the same issue):
Try adding ' "whitespace", ""  '  (w/o the single quotes!) to textscan's
parameters, i.e.:

tot = textscan (fid, '%s', 'delimiter', '\n', 'whitespace', '');  ## Not
sure if delimiter param is still required

It'll give you a cellstr array of lines (the exact lines from your data
file). AFAIU that is what you want to achieve, no?
If you want to go further, you'd have to split the data yourself along the
lines of:

tot = cell2mat (cell2mat (tot));    # gets you an array of strings
data1 = tot(:,  1:11);
data2 = tot(:. 12:22);
:
etc

Philip



--
View this message in context: 
http://octave.1599824.n4.nabble.com/textscan-preserving-all-whitespace-tp4666470p4666472.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

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