Search code examples
matlabcsvsave-as

CSV files refuse to be read by Matlab


I am currently writing a matlab program and the initial stage involves calling up .csv files from within folders. For reasons unkown, matlab will not read the files (checked using csvreader, dataimport and fopen). Note-it is definitely a csv file!

However, I opened one of the files, pressed 'save as', gave it the same name and fileformat. The only noticeable thing that happened is that the filesize reduced significantly and then matlab could magically open it but I have no idea why.

Can anyone shed any light on why this is happening? I would just open and resave the files except the data is related to a large number of samples which would make the manual process very long. If it is of relevance, the data is outputted from an Instron.

Many thanks :)

EDIT So this is a sample of one of the files called '2mm.csv', opened using Notepad (first 10 lines of ~111,000):

Time,Extension,Load
(s),(mm),(N)
"0.00000","51.97554","0.09549"
"1.00000","52.13438","0.24999"
"2.00000","52.30102","0.13996"
"3.00000","52.46782","0.19513"
"4.00000","52.63449","0.15348"
"5.00000","52.80097","0.26828"
"6.00000","52.96780","0.32510"
"7.00000","53.13446","0.67119"
"8.00000","53.30105","4.56026"
"9.00000","53.46772","17.80811"

This is code I use to open it and the result:

>> importdata('2mm.csv',',',2)

ans = 

    'Time,Extension,Load'
    '(s),(mm),(N)'

Note that it has only captured the first 2 lines and has not delimited the comma.

So I opened the file in MS Excel, saved it as 2mmv2.csv and put the same code in. I was was given a structure as expected:

>> importdata('2mmv2.csv',',',2);
>> ans.data(1:10,:)

ans =

         0   51.9755    0.0955
    1.0000   52.1344    0.2500
    2.0000   52.3010    0.1400
    3.0000   52.4678    0.1951
    4.0000   52.6345    0.1535
    5.0000   52.8010    0.2683
    6.0000   52.9678    0.3251
    7.0000   53.1345    0.6712
    8.0000   53.3010    4.5603
    9.0000   53.4677   17.8081

While I can now call up the file, I am none the wiser as to why this is the case.


Solution

  • Try this:

     file=fopen('test.csv');
     c=textscan(file,'%f%f%f','HeaderLines',2,'CollectOutput',true, ...
         'delimiter', {',','"'},'MultipleDelimsAsOne',true);
     fclose(file);
     dat=c{1}