Search code examples
matlabdelimiter

matlab: delimit .csv file where no specific delimiter is available


i wonder if there is the possibility to read a .csv file looking like:

0,0530,0560,0730,.... 90,15090,15290,157....

i should get:

0,053 0,056 0,073 0,... 90,150 90,152 90,157 90,...

when using dlmread(path, '') matlab spits out an error saying Mismatch between file and Format character vector. Trouble reading 'Numeric' field frin file (row 1, field number 2) ==> ,053 0,056 0,073 ...

i also tried using "0," as the delimiter but matlab prohibits this.

Thanks, jonnyx


Solution

  • You can actually do this using textscan without any loops and using a few basic string manipulation functions:

    fid = fopen('no_delim.csv', 'r');
    C = textscan(fid, ['%[0123456789' 10 13 ']%[,]%3c'], 'EndOfLine', '');
    fclose(fid);
    C = strcat(C{:});
    output = strtrim(strsplit(sprintf('%s ', C{:}), {'\n' '\r'})).';
    

    And the output using your sample input file:

    output =
    
      2×1 cell array
    
        '0,053 0,056 0,073'
        '90,150 90,152 90,157'
    


    How it works...

    The format string specifies 3 items to read repeatedly from the file:

    • A string containing any number of characters from 0 through 9, newlines (ASCII code 10), or carriage returns (ASCII code 13).
    • A comma.
    • Three individual characters.

    Each set of 3 items are concatenated, then all sets are printed to a string separated by spaces. The string is split at any newlines or carriage returns to create a cell array of strings, and any spaces on the ends are removed.