MATLAB: Using fopen and fscanf in loop

fopenfscanf

Hi everybody I have 1000 output files with time and acceleration. accel(1).out to accel(1000).out I have to remove all time columns from all files and save the maximum accelerations in a matrix.
this simple code can do it for 3 files but when I do it in loop it gives error, probably the problem is with fscanf part, any suggestion???
i=1; j=2; k=3;
fid(i) = fopen(['accel(',num2str(i),').out'],'r');
fid(j) = fopen(['accel(',num2str(j),').out'],'r');
fid(k) = fopen(['accel(',num2str(k),').out'],'r');
[a] = fscanf(fid(i), '%f' , [2 inf]);
[b] = fscanf(fid(j), '%f' , [2 inf]);
[c] = fscanf(fid(k), '%f' , [2 inf]);
fclose(fid(i));
fclose(fid(j));
fclose(fid(k));
a(1,:) = [];
b(1,:) = [];
c(1,:) = [];
acc_matrix = [a' b' c']
====================================
the way i do it in loop which is wrong !!!
ii = 1:3;
fid(ii) = fopen(['accel(',num2str(ii),').out'],'r');
[a(ii)] = fscanf(fid(ii), '%f' , [2 inf]);
fclose(fid(ii))
many thanks

Best Answer

You need to have the word "for" before the "ii=1:3", and you don't need the semicolon at the end of that line, and you need an "end" at the end of the loop. Then you're reading a bunch of values into just one element, a(ii), of the array. That won't work. YOu'd need to preallocate a and read the values into a row, a(ii, :) - note the comma and semicolon.