Why is "Data Import" not correctly importing a CSV-file with different row-lengths?

2 次查看(过去 30 天)
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];

采纳的回答

MathWorks Support Team
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 个)

类别

Help CenterFile Exchange 中查找有关 Geoscience 的更多信息

产品


版本

R2008b

Community Treasure Hunt

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

Start Hunting!

Translated by