1

I have a CSV file that has 3 columns having : String type(This can be a word or a sentence), integer type, integer type [these are the column types]
Now I want to store this data into a matrix,where the string(the whole sentence is stored into one cell)and the integers in one cell each too. I tried this:

fileID=fopen('training.csv');
C=textscan(fileID,'%s %d %d');

But it gives each word of the sentence in a different cell!The whole sentecne should be stored in one cell.How do I do that?

Thank you.

EDIT:It does seem to read anything now,this is what I get

celldisp(C)

C{1}{1} =

     jdl 

C{2} =

     []   

C{3} =

     []

EDIT:

jdl h-yf u ghjktnfhcrjuj hjcljd-yf-ljye                 129771  196
EAS CJDTNCRBH YFIRJHNJCNFY UJH HTDL HTCG                819100  458
rcfcyjzcrjuj rfycrf u rhfz edl                          547653  677
trfcthbyyehuf h-yt jnltkjv eavc xrfkjdcrjv u hjccbb d   970121  884
H-YF TRFNTHBYYEHUF U EDL XRFKJDCRJUJ                    938870  630
jdl yfhyfek h-yf rtktpyjljhjryjuj                       525855  598
rhfcyjzhcrf eghfdktybtv dyenhtyybq hfqjyf u ktybycrjuj  709215  403

Pretty much looks like this.It has 3500 rows of data

Ray
  • 2,472
  • 18
  • 22
LoveMeow
  • 3,858
  • 9
  • 44
  • 66

2 Answers2

1

You could put the strings into quotation marks and then use %q in textscan:

%q  String, where double quotation marks indicate text to keep together

EDIT: If you only need to do that once (and not automatically via script in another process), what about manually importing the data from the CSV-File into MATLAB (via the import-tool provided by matlab:

enter image description here)

and then maybe save the imported variables simply to a MAT-file so you can access it easier in the future!?

tim
  • 9,896
  • 20
  • 81
  • 137
  • coukd you elaborate,I dont undertsand what you are saying.Should I modify the file I have? the file has 1000 rows. – LoveMeow Dec 16 '13 at 12:26
  • Yeah what I meant was to export the data into a csv-file where the strings are between quotation marks, e.g. look like something like this: "bla bla bla";1;2 and then you could use the parameter %q in textscan to match the strings inbetween quotation marks. But that would only be an option if you could modify the csv-file easily (e.g. by changing the output routine of the program which outputs the csv-file). Where does the csv-file come from (generated from what)? – tim Dec 16 '13 at 12:32
  • Given by my university.Its a training data i need to import into matlab to work on(Machine learning).It has 3 columuns and many many rows.The first column has a sentence.And im stuck wondering how i can import that first column as a first column in a matrix in matlab. – LoveMeow Dec 16 '13 at 12:34
  • Oh okay then it's probably not easily possible to convert the format. So this solution isn't for you. Could you upload the csv-file? – tim Dec 16 '13 at 12:37
  • Yeah I can.is it possible to upload here or should I use external link? – LoveMeow Dec 16 '13 at 12:42
  • Thank you! I used the import button ,but if I were to write a script would it still work to use the import button? – LoveMeow Dec 16 '13 at 12:44
  • 1
    If you'd want to always import the sama data within your trainig script, I sugest to manually import it once and save the imported data to a mat-file which you can easily load in MATLAB via `load('bla.mat')`. – tim Dec 16 '13 at 12:53
0

If you have Excel, you can use xlsread. You can also try importdata.

am304
  • 13,758
  • 2
  • 22
  • 40