Need help using textscan to import file

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!

 Respuesta aceptada

dpb
dpb el 15 de En. de 2015
Editada: dpb el 15 de En. de 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 comentarios

guywhoneedshelp
guywhoneedshelp el 15 de En. de 2015
Thank you for the response! This gave me close to what I wanted, but unfortunately it stores all the numbers in different rows. So my output for a whole file was 17262x1. Is there a way to separate the rows so that the output would be A = 1233x14?
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.
guywhoneedshelp
guywhoneedshelp el 15 de En. de 2015
Thank you! That works perfectly

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 15 de En. de 2015

Comentada:

el 15 de En. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by