jonnyx
jonnyx

Reputation: 347

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

Upvotes: 3

Views: 250

Answers (3)

gnovice
gnovice

Reputation: 125864

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.

Upvotes: 3

Pac0
Pac0

Reputation: 23174

If you have access to a GNU / *NIX command line, I would suggest using sed to preprocess your data before feeding into matlab. The command would be in this case : sed 's/,[0-9]\{3\}/& /g' .

$ echo "90,15090,15290,157" | sed 's/,[0-9]\{3\}/& /g'

90,150 90,152 90,157

$ echo "0,0530,0560,0730,356" | sed 's/,[0-9]\{3\}/& /g'

0,053 0,056 0,073 0,356

also, you easily change commas , to decimal point .

$ echo "0,053 0,056 0,073 0,356" | sed 's/,/./g'

0.053 0.056 0.073 0.356

Upvotes: 2

Sardar Usama
Sardar Usama

Reputation: 19689

str= importdata('file.csv','');   %importing the data as a cell array of char
for k=1:length(str)               %looping till the last line
    str{k}=myfunc(str{k});        %applying the required operation
end

where

function new=myfunc(str)
  old = str(1:regexp(str, ',', 'once'));  %finding the characters till the first comma
                                              %old is the pattern of the current line
  new=strrep(str,old,[' ',old]);  %adding a space before that pattern
  new=new(2:end);                 %removing the space at the start
end

and file.csv :

0,0530,0560,073
90,15090,15290,157

Output:

>> str

str=

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

Upvotes: 4

Related Questions