Why do I get a "sharing violation" error when I use my App to open, modify, and save an Excel file?

3 vues (au cours des 30 derniers jours)

I have a MATLAB App that opens up an Excel file in my local folder upon pressing a button. I am able to modify values, save it the first time I open it up and then close the file.
When I try to open the Excel file again using my button, I am able to open and modify the Excel file but I am not able to save this file. Upon trying to save, I see a dialog box saying the following: "Your changes could not be saved to "filename.xlsx" because of a sharing violation. Try saving to a different file".
How do I solve this?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 28 Mar 2024
In the callback function corresponding to the button that opens the Excel file, please verify that you have called "fclose" after "fopen". The callback function after correcting it should look something like this:
fid = fopen(FileName,'r');
% perform other actions here using the open file
fclose(fid);
The first time you press the button, you are able to save it since that is the first open instance of your Excel file. 
During the second time you open the file through your App's button, there is already an open instance of the same file inside the MATLAB process which prevents you from making changes to the file. This is despite you closing the Excel file window from the first instance. Calling "fclose" on the file identifier inside your callback function should solve the issue.
Please note that it is generally a recommended practice to call "fclose" on a file identifier opened using "fopen" after reading/writing to it in MATLAB.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by