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

3 次查看(过去 30 天)

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?

采纳的回答

MathWorks Support Team
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by