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

4 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2011-2-9
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

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by