Clear Filters
Clear Filters

how to open multiple files and write to those open files at the same time in a loop

7 views (last 30 days)
Hi I try to write three different two dimensional table of which the data is calculated in a single loop to a file dedicated to each table; hence three different files.
my code starts like this:
fid1 = fopen(filename1, 'w');
if fid1 == -1, error('Cannot open file'); end
fid2 = fopen(filename2, 'w');
if fid2 == -1, error('Cannot open file'); end
fid3 = fopen(filename3, 'w');
if fid3 == -1, error('Cannot open file'); end
Then i want to write somethings to the files like this :
%for loop where the essence looks like this:
for
[parameter_1,parameter_2,parameter_3] = calculations()
fprintf(fid1,'\t%g',parameter_1);
fprintf(fid2,'\t%g',parameter_2);
fprintf(fid3,'\t%g',parameter_3);
end
I get the error 'cannot open file' as soon as matlab tries open the second file (filename2). When i comment out the second and third line i get the same error for the third file (filename3); hence could it be that Matlab cannot open several files simultaniously to write to?
If this correct,is there an alternative way to circumvent this problem?
thanks!
  2 Comments
dpb
dpb on 17 Dec 2013
Not a limitation in Matlab, no.
Most likely either filename[2|3] is already open for another process during your debugging or similar problem.
Use the alternate second return to fopen to get more info on why the operation is failing...
[fid2,msg] = fopen(filename2, 'w');
msg
And, of course, make sure the path you're trying to write to is one for which you have write privilege and so on...

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!