excelmatlab

Close an Excel file was opened in Matlab


My code needs to open an Excel file (with formulas) to search for a given value and close this file, will do some calculations and will write the answer in this file. I can not get Matlab to close the file before it writes the response and this error appears: Error using xlswrite (line 226) The file C: \Temp\Data_Home.xlsx is not writable. It might be locked by another process.

File_123 = 'C: \Temp\Data_Home.xlsx';
xlObj = actxserver('Excel.Application');            %Start Excel
wsObj = xlObj.Workbooks.Open(File_123);             %Open workbook
Sheet = wsObj.Sheets.Item(3);                       %Assume we're using the first sheet

Solution

  • Use Quit and delete as mentioned in the example in the documentation of actxserver.

    Quit(xlObj);
    delete(xlObj);