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

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2013-12-17
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...

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by