xlsread()认为我的单行CSV有1048576行 [英] xlsread() thinks my 1-line CSV has 1048576 rows
问题描述
我想使用 [〜,〜,temp] = xlsread('1.csv','A:A');
1行CSV文件 1.csv
。
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
只包含一行:
5B0E8795E18013D0FBC33558F0512832,3,7,1,Practice,Juicer,Juicer,true,false,2347.0, 0
5B0E8795E18013D0FBC33558F0512832,3,7,1,Practice,Juicer,Juicer,true,false,2347.0,0
但是返回的 temp
是一个< 1048576x1>单元格。不应 temp
是<1x1>单元格?
However the returned temp
is a <1048576x1> cell. Shouldn't temp
be a <1x1> cell?
参数'A :A'
应该只返回第一列的现有行,如 xlsread()文档。由于 temp
是一个< 1048576x1>单元格,看起来像使用'A:A'
返回整个列,包括不存在的行(1048576是Microsoft Excel 2010中的最大行数)。
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).
使用 textscan()
works fine(= datatemp
在以下代码段中只有1行):
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);
$ b c>无法正常工作。我使用MATLAB R2012a 64位,Microsoft Excel 2010和Windows 7 x64。
However I don't understand why xlsread()
fails to work properly. I use MATLAB R2012a 64-bit, Microsoft Excel 2010 and Windows 7 x64.
推荐答案
这是Excel COM接口的工作原理,所以你不能指责MATLAB:)
That's actually how the Excel COM interface works, so you can't blame MATLAB :)
这里是一个基本上在内部实现 xlsread
的示例代码。您可以在VBScript / Powershell中编写代码,并获得相同的结果...
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();
返回变量 val
:
>> whos val
Name Size Bytes Class Attributes
val 1048576x1 71303224 cell
其中除第一个之外的所有单元格都是NaN:
where all cells except the first one are NaNs:
>> val(1:3)
ans =
'5B0E8795E18013D0FBC33558F0512832'
[NaN]
[NaN]
我不明白为什么你不使用 textscan
来解析文件文本,这比调用COM快得多(更不用说可以移植到除Windows之外的其他平台)
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)
这篇关于xlsread()认为我的单行CSV有1048576行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!