I'm trying to read data from a .txt file. The sample data is given below. I need an algorithm to read just M-N row numbers. While I can use while/for loops, I'm afraid that it might become very slow. Thanks!
a=[ 1 6 11 16 ;
2 7 12 17 ;
3 8 13 18 ;
4 9 14 19 ;
5 10 15 20] ; % data is in Test.txt -->
% fid = fopen('Test.txt');
% a=a.'; fprintf(fid, '%.3f\t%.3f\t%.3f\t%.3f\r\n', a(:)) ;
fid = fopen('Test.txt') ;
AnsMat = fscanf(fid, '%f %f %f %f')
AnsMat = [2 7 12 17 ; 3 8 13 18] ; % Read row-numbers 2 to 4 this time
You could try
textscan
which allows aHeaderLines
parameter telling matlab how many lines to skip.For example to read lines n (=2) to m(=4), you could do:
This does return the data as a cell array though so you have to convert it: