Why is there a permission error when repeat writing to excel file?
308 views (last 30 days)
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.
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);
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);
Phuoc Ho on 2 Oct 2021
hey guy, i did exit that .xlxs and run code , then code havent error , you should try it
Quoc Vo on 4 Nov 2022 at 21:58
I had similar issue. I figured out that it was because the files I am working on was on OneDrive and there is conflict between writing command from MATLAB and synchronizing command from OneDrive. I temporarily turned off the synchronizing function of Onedrive and it works fine.
One more thing to note is that this issue only happen when I run the script on Windows. It works fine in MacOS without turning off OneDrive.
Hope that this would help.
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.
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.
NDuclaux on 9 Sep 2020
I get the same issue with one script. In my case the file created with writetable is incremental so there are mutiple writing event.
The script work for a while and then the bug apear at some point.
Also this issue appera on specific and fast laptop.
Putting a breakpoint on this line and pressing continue, the issue doesn't occur.
THerfore I think there is an issue at least in my case where there is not enough time between 2 writing attemp to refesh the status of the file, and it's still seen as being open, while it attempt to add information into it.
Having said that, I haven't find a fix yet.
Any idea how to make sure the file is actually close before trying to write on it?
nobrain on 20 Jan 2021
I was running into the same issue. Writing multiple times to the same excel spreadsheet with either writematrix or writecell eventually results in a permission denied error. Some more docu would be great on this issue.
I don't understand how functions can be deployed which cannot repeatedly be called??
I can suggest two workarounds which solved the issue for me:
Either use the windows excel. Be aware that, for me, this resulted in much more execution time than before.
writematrix(header, fullFileName, "Sheet", mirror.serialnumber, "Range", "C1", 'UseExcel', true);
or close all files after a some calls to a write function.
In my case, I have a function writing to a spreadsheet with multiple writematrix calls. A single call of this function works fine, calling the function multiple times on the same sheet resulted in an error. Closing alll files at the end of the function resolved the issue.
In all cases I used the full file name.
Hope this helps someone.
DocWalo on 29 Mar 2021
I don't know if I help to advance the problem but on my own experience to allow my script to write my excel I need to close all my workbook I am currently using to go through the script. If not I have the same error as describe in the orignal post.
John Draper on 10 Dec 2021
I had the same issue. Interestingly, the error occured shortly after I switched to an open excel document. I think excel might be restricting permissions when the executable is running (i.e., when you have any .xlsx file open).
In short: closing all excel files and rerunning the script worked fine for me.
Justin Curtiss on 29 Dec 2021
I had a similar issue where I was creating a new sheet in a loop and writing it to the same excel file, 30 sheets total. Adding a pause at the bottom of the loop worked, 2 seconds. Not efficent, but solved the issue