How to read a text file with multiplel matrix data sets listed one after another
Mostrar comentarios más antiguos
Below is an example (in the real world, I have hundreds of data sets in a text file):
How do I load the data into Matlab?
Thank you in advance.
2 comentarios
Walter Roberson
el 18 de Ag. de 2011
It won't let me see that page :(
Liqing
el 22 de Ag. de 2011
Respuestas (3)
Liqing
el 25 de Ag. de 2011
0 votos
Friedrich
el 25 de Ag. de 2011
Hi,
trz
fid = fopen('sample.txt','r');
out = textscan(fid,'%s %s %s \r\n %f %f %f \r\n %f %f %f \r\n %f %f %f \r\n %f %f %f \r\n');
fclose(fid);
celldisp(out)
It depends on your OS if you have to use \r (MAC) or \n (Unix) or \r\n (Windows)
2 comentarios
Liqing
el 25 de Ag. de 2011
Walter Roberson
el 25 de Ag. de 2011
textscan by default ignores both \r and \n as being "Whitespace", so you can ignore the terminator issue:
textscan(fid, [repmat('%s',1,3) repmat('%f',1,12)]);
Fangjun Jiang
el 25 de Ag. de 2011
What is your desired output? Do you care about the text strings? The overall approach is to use textscan(). You can type help textscan or doc textscan for details.
fid=fopen('sample.txt','rt');
data=textscan(fid,'%f','CommentStyle','Depth');
fclose(fid);
data{1}
4 comentarios
Liqing
el 25 de Ag. de 2011
Fangjun Jiang
el 25 de Ag. de 2011
What version of MATLAB do you have?
Fangjun Jiang
el 25 de Ag. de 2011
You can use fgetl() to read the file line by line. If the line contains comma, read it as string (%s), otherwise, read it as double (%f). There is really no hard part. It's just tedious. If you just have one file to read, it might worth to convert it to a .xls file and then use [Num,Txt,Raw]=xlsread().
Fangjun Jiang
el 25 de Ag. de 2011
Here is an example.
http://www.mathworks.com/matlabcentral/answers/13383-how-to-extract-numerical-datafrom-txt-file-with-mixed-string-and-numerical-data
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!