Serg
Serg

Reputation: 14118

actxserver communication between Matlab and Excel

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:

  1. How to insert values into an opened excel file from Matlab, so the new values become immediately visible?

  2. How to get into Matlab the updated (from Excel) values?

Upvotes: 0

Views: 5335

Answers (3)

Sam Roberts
Sam Roberts

Reputation: 24127

actxserver will 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 actxGetRunningServer to connect to the running copy of Excel that is already visible with your file preloaded.

Upvotes: 1

Andy Raddatz
Andy Raddatz

Reputation: 2892

There is no ActiveSheet.get function, try using

range = sheet.Range("A1:B2")

Then for reading, you should be able to call

range.Value 

or

range.Text

Upvotes: 0

Serg
Serg

Reputation: 14118

This resolves both issues:

excelapp.Visible = 1;

Upvotes: 0

Related Questions