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

2 vues (au cours des 30 derniers jours)
Ruwan
Ruwan le 17 Déc 2013
Commenté : Simon le 17 Déc 2013
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 commentaires
dpb
dpb le 17 Déc 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...
Simon
Simon le 17 Déc 2013
In addition: Use
fclose('all')
to close all open files

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by