0

I have a csv file like this format:

2.3 , 1.3 , 1.2 , 6.8 , classone
1.2 , 2.6 , 1.8 , 0.7 , classtwo

I want to read the file into seprate matrix; the first 4 numeric values in one matrix and the string value in another matrix

I have tried textscan function but it doesnt work well

M= textread('training.dat','%f %f %f %f %s');

Error using dataread
Number of outputs must match the number of unskipped
input fields.
Peace
  • 17
  • 10
  • Have you seen the [`csvread`](http://www.mathworks.co.uk/help/matlab/ref/csvread.html) function? Not sure if you can get the string data with this though... – wakjah Apr 14 '13 at 20:17
  • @Eitan T Indeed, there is not much of a difference. The only trick was to take into account the spaces around commas in field delimiter. –  Apr 14 '13 at 21:34

1 Answers1

0

Try  

[num, str, ~] = xlsread('training.dat');

fid = fopen('training.dat');
D = textscan(                                ...
        fid,                   '%f%f%f%f%s', ...
        'Delimiter',           ' , ',        ...
        'MultipleDelimsAsOne', true          ...
);
fclose(fid);

numeric_stuff = horzcat(D{1:4});
string_stuff  = D{5};
  • Thank you for your help but It gives me two matrix: num with 0 elements and str with cell array and each cell contains the five elements without seprating numbers and strings – Peace Apr 14 '13 at 20:01
  • Alraight, that's... unexpected. I'll come back with something else, just let me try it first. –  Apr 14 '13 at 20:10
  • @Peace Ok, I tried something else, in order to deal with those peculiar spaces around the commas... I works correctly on the data you provided in the example. Please note that if you insist on having the strings as a matrix, and not as a cell array, you may always apply `char()` to your cell array of strings. –  Apr 14 '13 at 20:39
  • @Peace: then the format you're showing is not exactly what you pasted. If I copy-paste your example data into a text file training.dat I can simply use CST-Link's code and it works fine. – Oleg Apr 14 '13 at 22:43