reading whitespace in textscan

5 次查看(过去 30 天)
Ben
Ben 2013-6-12
Hi,
I have some data to read in fixed field format as date/time (ddmmyyyyHHMM) then 4 data fields, as per this example:
24 91987 530 0.8455 0.7200 -0.1255 -0.1255
30 919872345 0.8329 0.7500 -0.0829 -0.0829
1101987 0 0 0.8687 0.8000 -0.0687 -0.0687
My code is:
resarr=textscan(fidres,'%2f%2f%4f%2f%2f%10.4f%10.4f%10.4f10.4f','whitespace','','delimiter','/n');
res= [datenum([resarr{:,3},resarr{:,2},resarr{:,1},resarr{:,4},resarr{:,5},zeros(size(resarr{:,1},1),1)]),resarr{:,6},resarr{:,7},resarr{:,8},resarr{:,9}];
I'd like to read the data and convert the date to datenum. But am having a lot of trouble with the whitespaces.
eg the first line gives 24,91,987,53,0,0.8455,0.7200,-0.1255,-0.1255
instead of 24,9,1987,5,30,0.8455,0.7200,-0.1255,-0.1255
How should I change the textscan?

回答(1 个)

Hugo
Hugo 2013-6-12
I can only think of two options:
1) Add spaces separating the fields in the data, i.e. instead of ddmmyyyyHHMM, modify the data so that you have dd mm yyyy HH MM. That should be easy.
2) The other way is to scan for text
resarr=textscan(fidres,'%2s%2s%4s%2s%2s%10.4f%10.4f%10.4f10.4f','whitespace','','delimiter','/n');
and then convert the text to numbers using, for example, str2double or str2num

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by