xlsread() thinks my 1-line CSV has 1048576 rows

2020-07-18 06:50发布

I wanted to use [~, ~, temp] = xlsread('1.csv','A:A'); to get the first column of the 1-line CSV file 1.csv.

1.csv contains just one line:

5B0E8795E18013D0FBC33558F0512832,3,7,1,Practice,Juicer,Juicer,true,false,2347.0,0

However the returned temp is a <1048576x1> cell. Shouldn't temp be a <1x1> cell?

The parameter 'A:A' should return only the existing rows of the first column, as shown in the "Read a Column of Data" example in the xlsread() documentation. Since temp is a <1048576x1> cell, it seems like using 'A:A' returns the entire column, including the non-existing rows (1048576 is the maximum number of rows in Microsoft Excel 2010).

Using textscan() works fine (= datatemp in the following snippet will have only 1 row):

fid = fopen('1.csv','r');
datatemp = textscan(fid, '%s %d %d %d %s %s %s %s %s %d %d', 'delimiter',',', 'CollectOutput',true)
fclose(fid);

However I don't understand why xlsread() fails to work properly. I use MATLAB R2012a 64-bit, Microsoft Excel 2010 and Windows 7 x64.

1条回答
姐就是有狂的资本
2楼-- · 2020-07-18 07:05

That's actually how the Excel COM interface works, so you can't blame MATLAB :)

Here is a sample code that basically does what xlsread internally. You could write the code in VBScript/Powershell and get the same result...

%# create Excel COM server
Excel = actxserver('excel.application');

%# open file
Excel.workbooks.Open(which('1.csv'), 0, true);
Excel.Worksheets.Item(1).Activate();
Excel.Visible = true;

%# select first column
Excel.Range('A:A').Select();
val = Excel.Selection.Value();

%# close
Excel.Quit();
Excel.delete();

The variable val returned:

>> whos val
  Name            Size               Bytes  Class    Attributes

  val       1048576x1             71303224  cell               

where all cells except the first one are NaNs:

>> val(1:3)
ans = 
    '5B0E8795E18013D0FBC33558F0512832'
    [NaN]
    [NaN]

I dont understand why you dont just use textscan to parse the file as text, which is much faster than invoking COM (Not to mention portable to other platforms other than Windows)

查看更多
登录 后发表回答