MATLAB Answers


best way to import this csv

Asked by Tom
on 14 Dec 2012
I can't work out how to import this csv file -
Have tried various methods but I find a lot of the functions and terms are brand new to me so a lot of the forum posts and help files I'm reading are just over my head.


Walter Roberson
on 14 Dec 2012
Which fields of it do you need?
You will not be able to use csvread() as that is only suitable for files that are completely numeric.
on 14 Dec 2012
All of it is important but only some of it represents actual results, i.e. some of them are more 'descriptors' (don't know proper language).
The IDs are for the 41 test subjects. HL & Spkrs1-Spkrs20 are descriptors. HL is linked to ID always, whereas Spkrs1-Spkrs20 are linked to the relevant values in for c1-c20 and AB1-AB20.
e.g. The first test on the first subject gives: - ID = 1 (that subject's ID number) HL = 1 (they're in class 1 not class 0) Spkrs1 = lrc2-lr (subject was subjected to lrc2 followed by lr) AB1 = 0 (they rated lrc2 above lr) c1 = 7 (on a 1-10 scale, they rated lrc2 to be at 7 compared to lr)
So ID, HL & Spkrs are all descriptors whereas AB & c are actual results from the experiment.
(It might be simpler for me to get rid of the AB column and just have the c values are going negative for when AB = 1)

Sign in to comment.


2 Answers

Answer by Walter Roberson
on 15 Dec 2012
 Accepted Answer

fid = fopen('Data.csv', 'rt');
fmt = ['%f', '%f', repmat('%f', 1, 20), repmat('%f', 1, 20), repmat('%s', 1, 20)];
dat = textscan(fid, fmt, 'HeaderLines', 1, 'Delimiter', ',');
ID = dat{1};
Hl = dat{2};
C = cell2mat(dat(3:22));
AB = cell2mat(dat(23:42));
spkrs = [dat{43:62}];
Now ID, H1, C, and AB are numeric arrays, with the C(:,1) corresponding to C1, C(:,2) corresponding to C2, and so on. And spkrs is now a cell array of strings with spkrs{J,1} corresponding to row J of columns spkrs1, and so on.


Sign in to comment.

Answer by per isakson
on 15 Dec 2012

This file may be imported with textscan, however, it requires a correct format string, i.e. one need get the number of column correct.
There are some tools in the file exchange, which might read the file. I would have tried "readtext" by Peder Axensten first.


Sign in to comment.