How do I fix writematrix error?

Hello, I am a beginner with MATLAB and am currently working on a code (part of it is legacy [from people outside my lab as I understand it], most is written by previous students) that involves simulating and saving results to an Excel sheet.
I am getting an error now, one that I never got at all since I started working (past 4 months) and I have been following the same procedure to simulate:
  1. Execute a script (This adds the path to current folder) that creates an initial excel file with variable names.
  2. Run the simulation script which runs for about 9 iterations and terminates once an optimal point has been found. The results are overwritten every iteration (I think, but not fully sure).
The error I get is:
Error using writematrix (line 206)
Unable to write to file 'D:\Applie Energy Simulations\AG_Opt-master\INRCD\Results\Uganda\Uganda_7.xlsx'. You may not have write permissions or the file may be open by another application.
Error in save_excel_data (line 25)
writematrix(value_2,file, 'Sheet', 'Results', 'Range', strcat(Column(Yr), string(location_2)));
Error in mg_cost (line 71)
save_excel_data(costPV,73, 2);
Error in mg_objfunction (line 6)
[myf,myc,myceq] = mg_cost(x_micro);
Error in sys_objfunction_eval (line 32)
y = mg_objfunction(x_micro); % calculate cost of microgrid subsystem
As I mentioned, I have been using the same in the past and have never received this error until recenty. I tried re-installing MATLAB R2021b, running MATLAB as admin, created a new directory with a copy of the code and ran it there. None fixed this issue. Please suggest how I can debug/ fix this. Appreciate any help!

7 Comments

when does the error appear? at the initial creation or in a specific iteration (last, first, other?)?
Hello,
The error appears at the last iteration. When I check the Excel file (after the error appears), I see values saved from the penultimate run, but NOT the final one (which is the answer we want as it is a converged solution).
The error message tells you:
"You may not have write permissions or the file may be open by another application."
Did you check this already? Is the file open? Do you have the privileges to edit or delete this file?
What has changed since the code ran the last time?
hello
you may have a conflict if the excel file is open while matlab is trying to write
@Jan Yes, I ensured that Excel was not running in the background. Used Task Manager to confirm that it was not running.
I am not sure how to check if I have priveleges to edit the file. The Excel file does not indicate any messages related to 'Enable editing' so I assume it's not locked.
What may have changed is - We have our code in Github and everytime new commits are made to it (the project is work in progress), I download the code as a .zip file and locally run the simulations on my PC. There are about 5-6 versions of the code (June update to Nov update), and I save each of these in separate folders. So there is no confusion with files being overwritten between repo versions.
Note- I tried re-running the old version just now and still got the same error. For the same version in the past, this has never happened.
@Mathieu NOE Hi! Since this question is being asked often, I will check - if the excel file is running in the background BEFORE the last results are saved.
I just know that there was NO ACTIVE EXCEL WINDOW open during the simulation and I had always checked that Excel was never running (in the background) AFTER the simulation ended with the errors. Is that the question you are asking? Please correct me if I missed something.
It sounds like you do not have write permission to the desired location. Try changing the write destination to somewhere else you have been able to write to in the past and see if the error goes away. You might also try running MATLAB as an administrator to see if that let's you write to that location.
In Windows, you can right click on the MATLAB Icon to select 'Run as Administrator'.
"I am not sure how to check if I have priveleges to edit the file." - open the file, edit it, save the file. If this does not show an error message, you have the privileges to modify the file.

Sign in to comment.

Answers (0)

Products

Release

R2021b

Commented:

Jan
on 10 Nov 2022

Community Treasure Hunt

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

Start Hunting!