필터 지우기
필터 지우기

How to read a text file with multiplel matrix data sets listed one after another

조회 수: 1 (최근 30일)
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
Liqing
Liqing 2011년 8월 22일
Problem fixed. Thanks for letting me know. Now you should be able to download the small txt file.

댓글을 달려면 로그인하십시오.

답변 (3개)

Liqing
Liqing 2011년 8월 25일
I am still waiting for an answer. Thanks.

Friedrich
Friedrich 2011년 8월 25일
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
Liqing
Liqing 2011년 8월 25일
Thank you very much. It works.
What if I have unknown and different number of matrix rows for each of them?
Walter Roberson
Walter Roberson 2011년 8월 25일
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
Fangjun Jiang 2011년 8월 25일
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
Fangjun Jiang
Fangjun Jiang 2011년 8월 25일
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
Fangjun Jiang 2011년 8월 25일
Here is an example.
http://www.mathworks.com/matlabcentral/answers/13383-how-to-extract-numerical-datafrom-txt-file-with-mixed-string-and-numerical-data

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by