Creating .csv file using fprintf - file is locked by 'another user'

4 vues (au cours des 30 derniers jours)
Alex
Alex le 9 Fév 2011
Hello, I am using fprintf to output variables of several different classes into a .csv file. I am using the following approach;
somedata={x x; x x; x x;};
[nrows ~] = size(somedata);
fid = fopen('myfile.csv','w');
for ii=1:nrows
fprintf(fid,'%f %f\n',somedata{ii,:}
end
fclose(fid)
When I go to open the .csv file in Excel, it claims that the file is locked for editing by 'another user'. I presume Matlab still has the file in memory or something which is making Excel cry.
How do I correct this?
thanks, Alex

Réponse acceptée

Jan
Jan le 9 Fév 2011
You closed the file correctly with FCLOSE. The problem is not included in the posted code. I assume, you have open the file before but did not close it, e.g. because the program stopped with an error. Then let Matlab close all open files:
fclose('all');
If this does not help, another application uses the file. Just restart the corresponding application and try to open the file in Excel again.
Ever check, if FOPEN could open the file:
fid = fopen('myfile.csv','w');
if fid == -1, error('Cannot open file'); end
  1 commentaire
Alex
Alex le 10 Fév 2011
The error check seemed to do it, thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by