Why readtable() function cannot read an xlsx file properly?
41 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
zhehao.nkd
el 2 de Jun. de 2021
I have an xlsx file with multiple sheets, and each sheet have 558 rows of data.
To read data from a single sheet (e.g. the third sheet), I used
readtable("featurexlsx.xlsx",'Sheet',3);
But what I got is a table with only 469 rows. Alternatively, I saved this sheet as an independent xlsx file and then read it by readtable(), I still got a table with 469 rows. Instead, when I saved this sheet as an csv file, I got 558 rows.
I was wondering what caused this error, is it a bug of readtable()?
2 comentarios
Jan
el 2 de Jun. de 2021
Editada: Jan
el 2 de Jun. de 2021
Which Matlab vesion do you use? Do you have Excel installed?
For me your command imports a [557x147] matrix, because the first row is considered as columnnames. With:
a = readtable("featurexlsx.xlsx",'Sheet',3, 'ReadVariableNames', false);
I get a [558x147] matrix. Matlab R2018b, Win 10, Excel 2010.
Did you check, which lines are missing in your xlsx import compared to the CSV import?
Respuesta aceptada
Stephen23
el 2 de Jun. de 2021
Editada: Stephen23
el 2 de Jun. de 2021
This is caused by the different row lengths in sheet 3: the shortest row has just 7 non-blank cells, the longest row has 147 non-blank cells. Apparently such wildly differing line lengths is enough to confuse readtable's automagical detection algorithms.
You could define the range yourself (e.g. A:EQ) or tell readtable to import the entire used range**:
T = readtable("featurexlsx.xlsx",'Sheet',3,'Range','')
Using a matrix is anyway most likely much more appropriate for this data:
M = readmatrix("featurexlsx.xlsx",'Sheet',3,'Range','')
** Although the documentation states that this is the default behavior, this option appears to actually return the entire range as expected, unlike the default behavior. So clearly the default and this option are not the same.
3 comentarios
Walter Roberson
el 2 de Jun. de 2021
Also, it turns out that if you readtable() that the results differ slightly from what you get if you readcell():
format long g
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/639480/featurexlsx.xlsx';
data1 = readtable(filename, 'sheet', 3, 'range', '');
data2 = readmatrix(filename, 'sheet', 3, 'range', '');
data3 = readcell(filename, 'sheet', 3, 'range', '');
d1 = data1{1,1}
d2 = data2(1,1)
d3 = data3{1,1}
Notice the readcell() version is rounded. I do not know why that is happening at the moment.
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!