I just want to read data from a txt file,why it's unintelligible text,may anybody can help me??? thx
code :
filename='test2.txt';
fileID=fopen(filename,'r');
formatSpec='%d %d %d %d %d %d %d %d %d %s';
size=[10,inf];
A=fscanf(fileID,'%d %d %d %d %d %d %d %d %d %s',size);
fprintf(formatSpec,A);
fclose(fileID);
________________________________________________________________
txt data :
0 213 1038 241 1072 10000 1 0 0 "Biker"
0 211 1038 239 1072 10001 1 0 1 "Biker"
0 211 1038 239 1072 10002 1 0 1 "Biker"
0 211 1038 239 1072 10003 1 0 1 "Biker"
0 211 1038 239 1072 10004 1 0 1 "Biker"
0 211 1038 239 1072 10005 1 0 1 "Biker"
0 211 1038 239 1072 10006 1 0 1 "Biker"
0 211 1038 239 1072 10007 1 0 1 "Biker"
____________________________________________________________
result:

댓글 수: 5

madhan ravi
madhan ravi 2019년 4월 8일
Upload your text file.
wenhuan wu
wenhuan wu 2019년 4월 8일
ok ,can you help me :)
T = readtable('test2.txt');
table2cell(T)
amazing bro! but what wrong in my code?
Guillaume
Guillaume 2019년 4월 8일
As was pointed out to me by Peter recently, converting the table to a cell array is not a good idea. It's going to use a lot more memory for no useful gain. Keep the data as a table.
madhan ravi
madhan ravi 2019년 4월 8일
Noted with care :)

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

 채택된 답변

Guillaume
Guillaume 2019년 4월 8일

0 개 추천

what wrong in my code
You're using fscanf to read both numbers and text. it doesn't work very well for that. In particular, the documentation is clear:
"If formatSpec contains a combination of numeric and character specifiers, then fscanf converts each character to its numeric equivalent. This conversion occurs even when the format explicitly skips all numeric values (for example, formatSpec is '%*d %s')"
So you get a A matrix that is nothing but numbers with the text having been converted to numbers (character code of each character).
In addition, you're telling fscanf that the output should have 10 rows: 9 numbers + 1 string. Actually, in your text file, the output should have 16 rows: 9 numbers + 7 characters. However, if one of the line happened to have a string with a different number of characters, you would have a problem as you can no longer reshape the output in a matrix.
You can use indeed use readtable to read your text file. It's the simplest and most efficient way. If you want to use low-level functions, use textscan:
filename='test2.txt';
fileID=fopen(filename,'r');
formatSpec='%d %d %d %d %d %d %d %d %d %s'; %I'd use %q instead of %s
size=[10,inf];
filecontent = textscan(fileID, formatSpec);
fclose(fileID);
table(filecontent{:}) %pretty display

댓글 수: 1

wenhuan wu
wenhuan wu 2019년 4월 8일
thanks to tell me so much!your answer is very clearly,you have a very kind heart!

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

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2019년 4월 8일

댓글:

2019년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by