using fopen and fscanf in loop

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

 Akzeptierte Antwort

Image Analyst
Image Analyst am 22 Aug. 2013

0 Stimmen

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.

1 Kommentar

Image Analyst
Image Analyst am 23 Aug. 2013
Bearbeitet: Image Analyst am 23 Aug. 2013
Regarding your "Answer" below...I think that all your data is not 3 elements long. Try
pre_matrix = zeros(3000,3);
for ii = 1:3000
fid(ii) = fopen(['disp(',num2str(ii),').out'],'r');
a = fscanf(fid(ii), '%f' , [2 inf]);
size(a);
if size(a) == 3
pre_matrix(ii,:) = a;
end
fclose(fid(ii))
end
Even so, that is horrible, non-robust code. For example you don't even check if the file exists before you try to call fscanf(), you're making an array of file ID's when there is no need to do so, etc.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

reza
reza am 23 Aug. 2013
Bearbeitet: reza am 23 Aug. 2013

0 Stimmen

thank you for your help
I followed your comments, but it still gives error of Subscripted assignment dimension mismatch.
pre_matrix = zeros(3000,3);
for ii = 1:3
fid(ii) = fopen(['disp(',num2str(ii),').out'],'r');
pre_matrix(ii,:) = fscanf(fid(ii), '%f' , [2 inf]);
fclose(fid(ii))
end

3 Kommentare

Image Analyst
Image Analyst am 23 Aug. 2013
Is this an "Answer"???
reza
reza am 23 Aug. 2013
Bearbeitet: reza am 23 Aug. 2013
thank you it helped, for what I'm going to do is there any other way ? I'm saying this because you said its not robust!
Image Analyst
Image Analyst am 23 Aug. 2013
As another example of why this code is not robust, you're putting all your data into one row, the ii'th row. But you have only 3 columns because that's how you preallocated pre_matrix. Are you sure that each file you are reading has only 3 numbers in it? No, you didn't. Robust code would check for that, otherwise you'll throw an exception. Robust code would also use fullfile and sprintf to build up the filename. Robust code doesn't use disp() to create a string for use as a variable. Etc. I think you might want to read http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by