0

I have a big csv file (6.5millions x 25) and trying to load it to Matlab. I already tried xlsread , csvread but can't get satisfying results. I read I can achieve it by using textscan.

My data looks this way:

date_time;writetime;F1;F2;F3;R1;h12;b12;h_main;
01.01.2016 0:00:01;504910801075;1;1;1;3,94;799;1515;3,877;
01.01.2016 0:00:02;504910802314;1;1;1;3,96;795;1516;3,857;

First row is a header. Others rows are data. All other rows are of the same format.

My code:

fileID = fopen('value1.csv','r');
formatSpec = '%s; \n';

formatSpec1 = '%s%f %f %f %f %f %f %f %f %f %f\n';
A1 = fscanf(fileID, formatSpec);

A2 = textscan(fileID, formatSpec1,'Delimiter',{';', ','});

I read header to A1 it's ok:

A1 =
date_time;writetime;F1;F2;F3;R1;h12;b12;h_main;

And read data to A2 too.

A2 = 
    {1x1 cell}   [5.0491e+11]   [1]   [1]   [1]   [3]   [94]   [799]   [1515]   [3]   [877]

But how to read 3,94 values? Because it can be just 4 or 0,064 value in this column.

Hope at your help!

Community
  • 1
  • 1
Mikhail_Sam
  • 7,481
  • 10
  • 37
  • 71

1 Answers1

1

I would treat them as a string. The following works.

fileID = fopen('value1.csv','r');
formatSpec = '%s; \n';
A1 = fscanf(fileID, formatSpec);
formatSpec1 = '%s%f %f %f %f %s %f %f %s\n';
A2 = textscan(fileID, formatSpec1,'Delimiter',{';'});
A2{6} = str2double(strrep(A2{6},',','.'));
A2{end} = str2double(strrep(A2{end},',','.'));

{1x1 cell}    [5.0491e+11]    [1]    [1]    [1]    [3.94]    [799]    [1515]    [3.8770]
giosans
  • 1,060
  • 9
  • 26
  • 1
    I went the same way! Didn't heard about `strrep` before, thank you! – Mikhail_Sam Jul 19 '16 at 10:48
  • I have one refinement for your solution: we can't use `strrep( char(A2{12}), ',','.'); Error using strrep Input strings must have one row. ` But we can go `str2double( strrep( A2{12}, ',','.') );` – Mikhail_Sam Jul 19 '16 at 14:20
  • ? the size of A2 array is 9, right? Also, A2{end} is a string, right? – giosans Jul 19 '16 at 14:57
  • 1
    Oh, I failed with the indexes - in my question I showed simplified example, in my real data it is much larger. I meant `A2{6}` of course. `strrep` wants to get one row, but `char(A2{6})` is a column. – Mikhail_Sam Jul 19 '16 at 15:19
  • 1
    I see. `strrep` works on cells as well. Changed! – giosans Jul 19 '16 at 15:31