Xlsread () thinks my 1-line CSV has 1048576 lines

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

1.csv contains only one line:

5B0E8795E18013D0FBC33558F0512832,3,7,1, practice, juicer, juicer, true, false, 2347.0.0

However, the returned temp cell is <1048576x1>. Should temp be cell <1x1>?

The parameter 'A:A' should only return existing rows of the first column, as shown in the example “Reading a data column” in the xlsread () documentation . Since temp is a cell <1048576x1>, it seems that using 'A:A' returns the entire column, including nonexistent rows (1048576 is the maximum number of rows in Microsoft Excel 2010).

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

 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 do not understand why xlsread() not working properly. I am using MATLAB R2012a 64-bit, Microsoft Excel 2010 and Windows 7 x64.

+4
source share
1 answer

Actually, how the Excel COM interface works, so you cannot blame MATLAB :)

Here is an example of code that basically does what xlsread internally. You can 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(); 

Return variable val :

 >> whos val Name Size Bytes Class Attributes val 1048576x1 71303224 cell 

where all the cells except the first are NaNs:

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

I don’t understand why you just do not use textscan to parse a file as text, which is much faster than calling COM (not to mention portability to other platforms than Windows)

+3
source

Source: https://habr.com/ru/post/1482252/


All Articles