Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Kill Excel process created in Matlab

Date: 2022-12-23 11:03:35

Given that I write to a workbook, I currently kill all Excel processes so that my code works when I call it in a loop. 

 

xlswrite(path,values);
system('taskkill /F /IM EXCEL.EXE');

This makes me unable to run the code while I am working in another Excel file. How do I make it so that Matlab terminates only the Excel processes that itself created? 

Answers: 

This was a "feature" introduced somewhere around R2015b to speed up multiple writes to Excel... not very user/memory friendly!

The xlswrite documentation links to this MathWorks Support Answer to manually write to Excel using actxserver, you can then manually delete the COM object referencing Excel.

You can actually edit xlswrite and see that it uses matlab.io.internal.getExcelInstance, which does the same thing and creates a COM interface with actxserver.

A cheeky option would be to copy xlswrite, add the Excel variable it creates as an output, and Quit and delete it afterwards, as shown below. I don't advocate breaking any of The MathWorks' copyright ownership of that function.

A less cheeky option would be to create a comparable function based on the answer I linked above, for writing data only it would look something like this: 

 

function xlswriteClean( File, Data, Range )
% XLSWRITECELAN writes data like XLSWRITE, but deletes the Excel instance! 
%  XLSWRITECELAN (FILE,DATA,RANGE) writes the variable in
%  DATA to FILE, in the range specified by RANGE. 
%  RANGE is optional, defaults to "A1"
    % Obtain the full path name of the file
    % Could handle this more elegantly, i.e. 
    % this always assumes the current directory, but user might give a full path
    file = fullfile(pwd, File);
    % Open an ActiveX connection to Excel
    h = actxserver('excel.application');
    %Create a new work book (excel file)
    wb = h.WorkBooks.Add();
    % Select the appropriate range
    if nargin < 3
        Range = 'A1';
    end
    rng = h.Activesheet.get('Range', Range); 
    % Write the data to the range
    rng.value = Data; 
    % Save the file with the given file name, close Excel
    wb.SaveAs( File );  
    % Clean up - the point of this function
    wb.Close;
    h.Quit;
    h.delete;
end

You can customise basically everything within the new Excel workbook using the COM object h, so you could add any functionality which you use in xlswrite like sheet naming etc.


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com