Using ActiveX to import data from Excel to MATLAB

12 次查看(过去 30 天)
I can find so many examples of going from MATLAB to excel but not the other way around. I don't want to use xlsread because it's super slow and I want to read from multiple sheets within a file and using xlsread for each sheet individually for each file (there are so many files) would take hours. I know how to do the following:
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
Workbooks = Excel.Workbooks;
exlFile = Workbooks.Open(filename);
eSheets = exlFile.Sheets;
eSheet1 = eSheets.get('Item', sheetname);
eSheet1.Activate;
and then I try to get the range using:
colEnd = eSheet1.Range('A1').End('xlToRight').Column;
rowEnd = eSheet1.Range('A1').End('xlDown').Row;
and then taking the column numbers and converting to letters with numbers using char and dividing by 26. so my variable xlRange looks like 'A1:EO5000' for example because EO is the last column with data and 5000 is the last row with data but then it doesn't accept the following:
Range = get(eSheet1,'Range',xlRange);
out = Range.value;
and gives me Error: Object returned error code: 0x800A03EC which I looked up and it has to do with an improper range.
I know that cell EO5000 is empty because the later columns don't have as many rows as the first ones- but how can I go about reading all the data from the sheet without having problems with the range (and can I just avoid using the range? Is there a better way to do this?)

采纳的回答

Megna Hari
Megna Hari 2016-8-15
JK I realized my mistake. I assigned xlRange = ['''A1:',char('A'-1+rem),char('A'-1+rem2),num2str(rowEnd),'''']; because I thought it needed quotes to work.
It doesn't like the quotes and accepts the range as xlRange = ['A1:',char('A'-1+rem),char('A'-1+rem2),num2str(rowEnd)];

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by