open excel file until it is closed in matlab

You can use the actxserver function in MATLAB to open an Excel file and then use the invoke function to interact with it. Here is an example code that opens an Excel file and keeps it open until you close it manually:

main.m
% Create activeX control object
excel = actxserver('Excel.Application');

% Hide Excel window
set(excel, 'Visible', 0);

% Open Excel workbook
workbook = invoke(excel.Workbooks, 'Open', 'example.xlsx');

% Loop until workbook is closed
while workbook.saved == 0
    pause(1); % wait for 1 second
end

% Close workbook and quit Excel
invoke(workbook, 'Close');
invoke(excel, 'Quit');

% Delete activeX control object
delete(excel);
431 chars
21 lines

This code will open the example.xlsx file and keep it open until you close it manually. The saved property of the workbook object is set to 0 until you save and close the file, so we can use a while loop and pause function to continually check if the file is closed yet. Once it is closed, the loop will exit and the Excel application will be closed.

related categories

gistlibby LogSnag