How to read a text file line by line?

954 次查看(过去 30 天)
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
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
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.

Ahmed A. Selman
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
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
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
Ugur CAN KOR 2018-1-14
How can I read this file? matlab code?
  10 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by