Write matlab multiple output file

advertisements

I want to write output of code periodically in different files in some specific folder. Code I am using is as follows:

for i=1:m
% some other things
     if (mod(i,1000)==0)
      y=[1:dx:n_x;c_initial.'];
      fn = ['/home/alekhine' num2str(i) '.dat'];
     fid=fopen(fn);
      fprintf(fid,'%6.4f  %12.8f\n',y);

      fclose(fid);
      end
end

But I am getting error from Matlab as follows

Error using ==> fprintf
Invalid file identifier.  Use fopen to generate a valid file
identifier.

. What is wrong in the code? Any help will be appreciated.


You seem to be missing a path separator in the path generation:

fn = ['/home/alekhine' num2str(i) '.dat'];

Should be:

fn = ['/home/alekhine/' num2str(i) '.dat'];