Need help using textscan to import file

1 view (last 30 days)
Hello all, I have a doc file that has a 10 line header that I do not need imported to matlab. Following these lines, I have numerical values I need imported that are separated by commas and a double colon. I would like them to be imported as a matrix stored in a single variable --> # of rows by 14 columns. The format looks like this:
130578547943024301,93,82.1882,87.2253,7168.89,261.738,194.983::130578547943024301,136,85.3604,94.8827,8099.22,386.447,173.988
130578547943180589,95,81.0533,87.1715,7065.54,262.06,196.035::130578547943180589,134,93.5194,102.914,9624.42,389,173
..... and so on. The commas and colon essentially play the same role and are both delimiters separating the data.
For example I'd like the final output to be store in variable A which is a 1250x14 matrix.
Thanks in advance!

Accepted Answer

dpb
dpb on 15 Jan 2015
Edited: dpb on 15 Jan 2015
A=cell2mat(textscan(fid,'%f','headerlines',10, ...
'multipledelimsasone',1, ...
'delimiters',',:', ...
'collectoutput',1));
Use fopen to return a valid file handle fid first, of course...
  3 Comments
dpb
dpb on 15 Jan 2015
Hmmmmm....I was "underneath the impression" as a former colleague was wont to say that textscan would automagically return the number of columns in found in the file despite the single '%f' format string.
Two choices; either reshape the output or
fmt=repmat('%f',1,14);
A=cell2mat(textscan(fid,fmt,'headerlines',10, ...
'multipledelimsasone',1, ...
'delimiters',',:', ...
'collectoutput',1));
take your pick.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!