I have an existing (opened and empty) file “D:\api.xlsx” and need to communicate with it from Matlab.
First, I try to read values from A1:B2, and insert new values:
excelapp = actxserver('Excel.Application');
wkbk = excelapp.Workbooks;
wdata = wkbk.Open('D:\api.xlsx');
sheet = wdata.ActiveSheet;
range = sheet.get('Range', 'A1:B2');
range.Value
ans =
[NaN] [NaN]
[NaN] [NaN]
range.Value = magic(2);
>> range.Value
ans =
[1] [3]
[4] [2]
But I don’t see the changes in the excel. The range A1:B2 remains empty.
Similarly, when I manually insert new values into excel, the range.Value returns the old values.
So, there are two questions:
-
How to insert values into an opened excel file from Matlab, so the new values become immediately visible?
-
How to get into Matlab the updated (from Excel) values?
actxserverwill create a new, invisible copy of Excel, which you then need to load the file into, and explicitly make visible (you’ve discovered this, as your own answer makes clear).Alternatively, if you already have your file open in Excel, you can use
actxGetRunningServerto connect to the running copy of Excel that is already visible with your file preloaded.