I want to import a csv file (data.csv) which is structured as follows
Area,MTU,Biomass MW,Lignite MW
CTA|DE(50Hertz),"01.01.2015 00:00 - 01.01.2015 00:15 (CET)","1227","6485"
CTA|DE(50Hertz),"01.01.2015 00:15 - 01.01.2015 00:30 (CET)","","6421"
CTA|DE(50Hertz),"01.01.2015 00:30 - 01.01.2015 00:45 (CET)","1230","n/e"
CTA|DE(50Hertz),"01.01.2015 00:45 - 01.01.2015 01:00 (CET)","N/A","6299"
I've tried to use textscan
which works pretty well, but the process stops once the empty quotation marks are reached. The argument 'EmptyValue',0
does not work.
file = fopen('data.csv');
data = textscan(file, '%q %q "%f" "%f"', 'Delimiter', ',',...
'headerlines', 1,'TreatAsEmpty', {'N/A', 'n/e'}, 'EmptyValue',0);
fclose(file);
Any idea, on how to import the whole file.
textscan(file,'%q%q%q%q%[^\n\r]','Delimiter',',','headerlines',1);
worked just fine for me. You get values like: "01.01.2015 00:00 - 01.01.2015 00:15 (CET)" But those are trivial to write a separate parser for. Don't try to do it all in one step. That will cause you much pain and suffering. Break it up into simple single steps.
Also, I highly recommend right clicking your file in the "Current Folder" window in matlab, then selecting "Import Data" This makes importing CSV (or tab separated, or fixed width data files) trivial.