How can I import filtered Data from Excel into Matlab

5 visualizzazioni (ultimi 30 giorni)
Hello everyone, I´m trying to filter Data in an Excel Sheet via COM-Server and then importing it into Matlab. My Test-Sheet looks like this:
[Active] [x] [y]
0 1 1
1 2 4
1 3 9
1 4 16
0 5 25
0 6 36
1 7 49
0 8 64
0 9 81
1 10 100
0 11 121
1 12 144
1 13 169
1 14 196
And my Matlab-Code so far:
exl = actxserver('excel.application'); % create COM-Server
str = strcat(pwd,'\TestData.xlsx'); % Path of the Excel-File
exlWkbk = exl.Workbooks;
exlFile = exlWkbk.Open(str);
robj = exlFile.ActiveSheet.Columns.End(4); % Find the end of the column
numrows = robj.row; % And determine what row it is
dat_range = ['A1:C' num2str(numrows)]; % Read to the last row
exlFilter = exlFile.ActiveSheet.Range(dat_range).AutoFilter(1,'1') % filters the data to only show rows with the value 1 in the first column
exl.Visible = 1; % show Excel Sheet
Cells = exlFile.ActiveSheet.Range(dat_range).SpecialCells('xlCellTypeVisible').Value % import filtered data
The connection to the Sheet and the application of the filter works fine, however the variable 'Cells' only contains the first line of the Sheet (Cells = [Active][x][y]). But when I set the Sheet on visible, the filter is applied correctly and exlFile.ActiveSheet.Range(dat_range).SpecialCells('xlCellTypeVisible').Count returns the correct number of 27 cells...
Has anyone an idea how i can read the filtered data from the excel sheet?

Risposte (1)

Hans Meier
Hans Meier il 26 Feb 2016
Ok I found a much simpler solution by importing the data into a table:
%Create Table
Table = readtable('TestData.xlsx', 'Range',dat_range);
%FilterData
FilteredTable = Table(Table.Aktiv == 1,:);

Community Treasure Hunt

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

Start Hunting!

Translated by