How to read a text file line by line?
이전 댓글 표시
Hello
I have a complicated text file,it runs row by row, first clumn is time and the next colum is corresponding acceleration, similarily it has 5 coulns of time and fivi colums of correcpoding acceleration, e.g. is givin below. How can I read this file using a matlab script?
0000 .0495 .0042 .0386 .0085 .0263 .0127 .0262 .0169 .0153
.0211 -.0079 .0254 -.0080 .0296 -.0312 .0338 -.0312 .0380 -.0545
.0423 -.0545 .0465 -.0763 .0507 -.0764 .0549 -.0996 .0592 -.0996
.0634 -.1229
Thank You
Jetson
댓글 수: 1
Walter Roberson
2013년 4월 9일
The last line appears to have only one time/acceleration pair. Should that be treated specially? Or do you just want to matrix will times in one column and corresponding accelerations in the other?
답변 (3개)
Image Analyst
2013년 4월 9일
편집: Image Analyst
2013년 4월 9일
Use fgetl(). From the help:
Examples
Read and display the file fgetl.m one line at a time:
fid = fopen('fgetl.m');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
You might also want to look at the dlmread() function.
댓글 수: 1
Timothy Mathias
2019년 7월 22일
Update for Matlab R2019a
Ahmed A. Selman
2013년 4월 18일
Try selecting one dimension a time for each line, e.g.,
...
time=fscanf(fID,'%g',[1 1:2:inf]);
acce=fscanf(fID,'%g',[1 2:2:inf]);
...
댓글 수: 2
Walter Roberson
2013년 4월 18일
If you read with inf as a size, then you are going to read to end of file, in which case the second fscanf() is not going to have any file to read from.
The size argument of fscanf() can be a scalar or a vector of length 2, but it cannot be a vector with more than 2 element such as [1 1:2:inf]
Jan
2013년 4월 18일
@Ahmed: Did you try the code? What do you expect as result of 1:2:inf? It must be a vector with infinite length, which must be stored in an infinitely large memory.
In addition, like Walter has explained already, fscanf reads the file sequentially, such that the idea of importing the variables one after the other does not match the was Matlab works.
Ugur CAN KOR
2018년 1월 14일
0 개 추천
How can I read this file? matlab code?
댓글 수: 10
Image Analyst
2018년 1월 14일
I'd use fgetl() to read a line, then use sscanf() or textscan() to parse the line.
Ugur CAN KOR
2018년 1월 16일
thanks ,I am trying.
Image Analyst
2018년 1월 16일
How about this:
fullFileName = fullfile(pwd, 'canakkale.txt')
t = readtable(fullFileName)
GreyHunter
2020년 10월 6일
편집: GreyHunter
2020년 10월 6일
How can I pick a specific line which is going to depend on the character?
for example. we have 2 collumns with numbers and one with characters( perhaps names) how you can pick one specific line 1 or more and store the into a matrix?
MANISH R
2022년 9월 22일
GreyHunter, even i have the same problem. Have you found out?
Image Analyst
2022년 9월 22일
Image Analyst
2022년 9월 22일
Wow, where did you get that? I mentioned absolutely nothing about you wanting college assignment help and all I did was to ask you to start your own question with your own data so we could give you a customized turnkey solution, and I gave you some pointers to useful functions you might try in the meantime.
He asked for how to read a "complicated" file (presumably one in which readmatrix doesn't work), which is exactly what I showed in my answer. Evidently that is not what you want even though you said you have the same problem. But I'll try to remember not to bother you anymore.
Vipul Kumar
2022년 9월 27일
@MANISH R What’s with the language buddy. You should start your own question to get a more accurate solution. It may also help others with similar problems as yours and will avoid digression from original topic. Thanks
MANISH R
2022년 9월 28일
@Image Analyst Sorry about that reply.
MANISH R
2022년 9월 28일
@Vipul Kumar Thanks.
카테고리
도움말 센터 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!