Why is "Data Import" not correctly importing a CSV-file with different row-lengths?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 27 Giu 2009
Modificato: MathWorks Support Team
il 31 Mar 2016
I have a two-row data file with different numbers of elements in each row. The first row of data has 2 elements, and the second row of data has 5 elements.
CSVREAD imports the data as expected.
On the other hand, if I use the "Data Import" feature by going to: File->Import data, the data is imported as 3X2.
For example, use the file:
------ test.csv ------
1,1
2,2,2,2,2
-----------------------
The command:
A = csvread('test.csv');
imports
A = [1 1 0 0 0 ; 2 2 2 2 2];
as expected.
If the Import Wizard is used, the result is:
A = [ 1 1 ; 2 2 ; 2 2 ; 2 NaN];
Risposta accettata
MathWorks Support Team
il 31 Mar 2016
Data Import looks at the first row in the CSV-file to determine the number of columns. If a subsequent row is shorter, it will be padded with NaN's. If a subsequent row is longer, it will be wrapped in rows of a length equal to the length of the first row, and the remainder row will be padded with NaN's.
This behavior is different from the CSVREAD command, which does what you would expect as a user: it chooses a number of columns equal to the maximum row length in the CSV file and missing data is imported as zeros.
0 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!