Martin
Martin

Reputation: 55

Open MS Excel by Matlab

is there a way how to make Matlab open excel files directly in MS Excel? I do not mean just read data, but physically open MS Excel. I have a script that processes some data and saves it into .xlsm. This .xlsm contains auto_open macro which generates a report and saves it into another .xls. I want the process to be as automatic as possible. So far, when the matlab script ends, .xlsm file needs to be open manually. Is it possible to do it via Matlab? (The .m file and .xlsm are in the same folder.)

Upvotes: 4

Views: 15024

Answers (3)

Jonas
Jonas

Reputation: 74940

The simplest way to open (but not close) an Excel file is to use WINOPEN:

winopen('myFile.xlsx')

Upvotes: 5

Max
Max

Reputation: 2131

Yes, it is possible. Matlab can use Excel's Automation interface to do this and similar tasks. The following code loads Excel and then loads a workbook:

try
    w = actxserver('Excel.Application');     % Fails if Excel not installed
catch
    w = [];
end

if ~isempty(w)
    w.Workbooks.Open('D:\Documents\MATLAB\file.xlsx');
    % Your code here;
    w.Quit;
end
delete(w);

You will need to look at Microsoft's documentation for the Excel automation interface to figure out exactly what commands you need to send. Try starting here. I have used this technique to drive Word from Matlab in order to produce a report document, but I have never used Excel this way, so unfortunately I can't help you with the gory details!

Upvotes: 2

learnvst
learnvst

Reputation: 16195

Excel has a powerful COM interface that lets you control it from another application.

Look here for the matlab side of things

http://www.mathworks.co.uk/help/techdoc/ref/actxserver.html

And here for an example of putting it all together . . .

https://www.quantnet.com/forum/threads/interface-between-matlab-vba-excel.2090/

or here is another example

http://www.mathworks.co.uk/support/solutions/en/data/1-716EAM/index.html?solution=1-716EAM

Upvotes: 2

Related Questions