MATLAB Answers

Why is there a permission error when repeat writing to excel file?

179 views (last 30 days)
Udit Gupta
Udit Gupta on 5 Sep 2016
Commented: vinyas babu on 17 Dec 2019
I am writing results of multiple analyses to different sheets of the same excel file using "writetable" command. The first instance of execution completes successfully but any subsequent calls to write to the same file generates error.
Unable to write to file 'RegressionResults.xlsx'. You may not have write permissions or the file may be open by another application.
I am using Windows 10 Education Edition 64 bit, Matlab R2016a, and Excel 2016. I have checked using process explorer, and the only two processes accessing the excel file are matlab.exe and excel.exe (so conflicts with any antivirus application).
Can anyone suggest a solution or a workaround for the problem.

  3 Comments

Jan Siroky
Jan Siroky on 7 Dec 2017
I do face the same problem (Matlab 2016b, Win10, MS Office 2013). Killing of all Excel processes does not solve the problem. The only workaround I hade found is change of the file name. Note, that file with original name does not exists. There is more that 8 GB RAM available (no memory issues) when trying to write table.
Jan Siroky
Jan Siroky on 18 Jan 2018
Update
I do face such strange behavior:
This code fails (Unable to write to file 'ERRORS2.xlsx'. Ensure the file is a valid spreadsheet file and is not password protected.)
writetable(this.errors_report, 'ERRORS2.xlsx')
however, this code writes a valid xlsx file
writetable(this.errors_report, fullfile(pwd, 'ERRORS2.xlsx'))
I do not understand this.

Sign in to comment.

Answers (3)

Tom Salomon
Tom Salomon on 6 Dec 2018
Edited: Tom Salomon on 6 Dec 2018
I had this issue too, and even more bizzare. I have around 50 trials. When I tried saving the data from MATLAB to excel using writetable or xlswrite, the script failed if the trial number was larger than 9 (?!)
for i = 1:50
file_name = sprintf('trial_%02i_sess_1.xlsx',i);
writetable(table2write{i},file_name);
end
The code would run up until trial 9 and then end with an error: 'Unable to write to file.... Ensure the file is a valid spreadsheet file and is not password protected'. Changing the file name to include the path (using pwd) fixed it!
for i = 1:50
file_name = sprintf('%s/trial_%02i_sess_1.xlsx',pwd,i);
writetable(table2write{i},file_name);
end

  0 Comments

Sign in to comment.


Andrew Thoesen
Andrew Thoesen on 13 Jul 2019
Edited: Andrew Thoesen on 13 Jul 2019
I used task manager to kill all excel processes and I was able to get this problem to successfully resolve. I read somewhere else that someone reset their computer and it magically decided to work. There's no clear mechanism or cause-effect for this error, it seems like a "try everything" type deal.
Edit: Small correction. I believe it may be caused (for me at least) by attempting to write files when I have multiple scripts open that are both on path but one is not in the working directory. Use pwd and ensure that the working path is the same one you're running the write script from.

  0 Comments

Sign in to comment.


Daniel Rubin
Daniel Rubin on 25 Sep 2019
I have encountered similar problem and I think I was able to pin-point a specific bug in the writetable function. The permission error is generated when the string with the xls filename is exactly 255 chars long. It works for lengths of 254 and 256, but not for 255. I am working with 2017b Matlab version.

  1 Comment

vinyas babu
vinyas babu on 17 Dec 2019
I have an issue where in 2017a it working fine but has stopped working or to say not working in Win10 2018b...

Sign in to comment.

Sign in to answer this question.

Products