Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: textscan text file with multiple type values and space columns
Replies: 6   Last Post: Jul 24, 2014 3:08 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,250
Registered: 6/7/07
Re: textscan text file with multiple type values and space columns
Posted: Jul 18, 2014 9:32 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 07/18/2014 5:36 AM, lily wrote:
> Hi everyone,
> I have a problem in using textscan function. My text file has multiple
> type fields and some field value is space. e.g., tline1='A, , ,HH ,CCC1
> ,18. , ,ZI , , , ';
> format=['%s %s %s %s %s %f4.0 %f8.0 ', repmat('%s ',1,4)];
> I want to get the values corresponding to each field, C={'A'; ' '; ' ';
> 'HH '; 'CCC1 '; '18. '; ' '; 'ZI '; ' '; ' '; ' '};
> But the following codes don't give the wanted,
> C=textscan(tline,format,'delimiter',',');
> I think it might be caused by the space-value, but I don't know how to
> deal with this situation. And is there any ways to read and get the
> fixed columns of a text file? Thanks in advance.


Seems to work here excepting the last, empty but non-delimited field is
returned as empty instead of blank. If that's your complaint, try

>> tline1='A, , ,HH ,CCC1 ,18. , ,ZI , , , ';
>> fmt=[repmat('%s ',1,5) '%f %f' repmat('%s ',1,4)];
>> textscan([tline1 ','],fmt,'delimiter',',')

ans =
Columns 1 through 9
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}
[18] [NaN] {1x1 cell} {1x1 cell}
Columns 10 through 11
{1x1 cell} {1x1 cell}
>> ans{:}
ans =
'A'
ans =
{''}
ans =
{''}
ans =
'HH '
ans =
'CCC1 '
ans =
18
ans =
NaN
ans =
'ZI '
ans =
{''}
ans =
{''}
ans =
{''}
>>

--



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.