Trouble using textread to ignore certain elements in a .csv file (new to matlab!)
Show older comments
Hi all,
I am very new to MatLab, and need some help using textread to read a .csv file, ignoring certain elements. Below is a small example of the data.
timestamp,time completed,task,set_no,x,y,time trained,stars,guess,guess (display),trials complete,trials remaining
1375988040,"Thu, 08 Aug 2013 18:54:00 GMT",aud_spatial_match_crbi,10,3,0,255,4,6,6 syls,11,0 1375988312,"Thu, 08 Aug 2013 18:58:32 GMT",aud_spatial_match_crbi,10,3,0,262,5,6,6 syls,6,5 1375989376,"Thu, 08 Aug 2013 19:16:16 GMT",digit_span_crbi,0,0,0,768,2,5,5 objects,14,0
My goal is to have textread ignore GMT" in the dates/time, since matlab seems to not recognize this (08 Aug 2013 18:54:00 GMT") as a date/time. There is also the issue of the preceding "Thu, which MatLab considers its own field due to the extra comma. This would also be great to get rid of. In addition, I'm also trying to ignore the first line.
Here is what I've attempted so far: [timestamp, thu, timecompl, task, setnum, x, y, timetrained, stars, guess, guessdisp, trialscomp, trialsrem] = textread('BrainTEST.csv','%d %s GMT"%s %s %d %d %d %d %d %d %s %d %d','delimiter',',','headerlines',1,'whitespace','')
The variable "thu" is me trying to accommodate the extra comma.
I have been confronted with the following errors:
Error using dataread Trouble reading literal string from file (row 1, field 3) ==> 08 Aug 2013 18:54:00 GMT",aud_spatial_
Error in textread (line 175) [varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
Any suggestions would be greatly appreciated!
Cheers, Sean C.
1 Comment
per isakson
on 10 Aug 2013
Edited: per isakson
on 10 Aug 2013
Use textscan rather than textread, since doc says so.
Why not read the full date string with %q and remove the leading day and trailing GMT in a separate step?
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Aug 2013
'%s GMT' does not cause the '%s' to "back up" as necessary so that there the GMT can be there. %s reads as far as possible considering the whitespace and delimiter settings.
% 1375989376,"Thu, 08 Aug 2013 19:16:16 GMT",digit_span_crbi,0,0,0,768,2,5,5 objects,14,0
fmt = '%d%s%d%s%d%s%s%s%d%d%d%d%d%d%d%s%d%d'
fid = fopen(''BrainTEST.csv', 'rt');
datacell = textscan(fid, fmt, 'delimiter', ',', 'headerlines', 1);
fclose(fid);
timestamps = datacell{1};
timecompleteds = strcat(datacell{3}, {' '}, datacell{4}, {' '}, datacell{5}, {' '}, datacell{6});
tasks = datacell{8};
% and so on
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!