0

Probably this has been answered before, but i havent found the answer yet. Im running a code in MATLAB that gives me the amplitude of a certain file that i put as a input, what i tried to do is to run the code for all the files that i had in the folder and make an array of this amplitudes for each file, unfortunately the problem seems to be the fid, since matlab says that for fegtl, i must use an apropiate fid. Here is the part of the code thats causing the trouble:

D=dir('Datos/Banda V-variables/01');

for file=D' %//'
    if file.bytes > 1    
        fid=fopen(file.name,'r');
        i=1;
        while 1
            tline=fgetl(fid); %This is the line where the error appears
            if ~ischar(tline), break, end
            A{i}=tline;
            i=i+1;
        end
    end
end

At the end i just do a number of calculations of the data and give out a number. A number for each file. Since clearly this method is not working what do yo suggest?, in order to have an array of amplitudes for each file into the folder. Any help would be very much appreciated. Thank you.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • This has an answer here: http://stackoverflow.com/questions/11621846/loop-through-files-in-a-folder-in-matlab – Fantastic Mr Fox Jul 16 '14 at 02:54
  • Yes thanks i saw that answer, but the thing is that it didn't work for me because the load option didnt run, i was hoping to see if i could use another command for it. – user3781757 Jul 16 '14 at 02:56
  • everytime you enter into `while 1` loop, you have a new `fid`, so how is that a problem? Print your `file.name` every time and see if that is as expected. – Autonomous Jul 16 '14 at 04:56

1 Answers1

0

You need to provide the path for the fopen command

fid = fopen( fullfile( 'Datos', 'Banda V-variables', '01', file.name ), 'r' );

Don't forget to fclose(fid) at the end!

You can also try and print the error if fopen fails

[fid err] = fopen( fullfile( 'Datos', 'Banda V-variables', '01', file.name ), 'r' );
if ~isempty(err)
   fprintf(2, 'Cannot open %s! Got error:\n%s\n', file.name, err );
end
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thank you !!!! it worked, clearly i didnt close the fid and thats what was causing the trouble. Thank you all, you all guys are geniuses !!!!! – user3781757 Jul 19 '14 at 18:20