MATLAB: Convert complicated csv to matlab, multiple dilimiters and implement strings to date and time value

csvdate conversionmultiple dilimiter

I created a csv export function in a c++ program, i reveived a file like "example.csv".
So i have a date, which is written in a strange format where every column looks like this
" Fr Apr 5 06:19:18 2013" ;" 7.58481"
" Fr Apr 5 06:19:21 2013" ;" 7.58481"
" Fr Apr 5 06:19:23 2013" ;" 7.58481"
The first value is a date and the second is a voltage. I Want to have two arrays out of the two columns which i can plot. The first still should be a date with time and the second a simple double.
I am new to matlab and tried with textscan function, but i am not able to use it right. Thanks for Help!

Best Answer

As you are new to MATLAB I will assume that you have a somewhat recent MATLAB.
%read the data
fid = fopen('Example.csv','rt');
datacell = textscan(fid,'%q%q', 'Delimiter',';', 'HeaderLines', 1);
fclose(fid);
date_strings = datacell{1};
voltage_strings = datacell{2};
%some lines are not complete
mask = cellfun(@isempty, date_strings) | cellfun(@isempty, voltage_strings);
date_strings(mask) = [];
voltage_strings(mask) = [];
%convert voltages
voltages = str2double(voltage_strings);
%convert dates
%your month name abbreviations appear to correspond to German month names.
%However, your date format is not in any of the accepted German standards
%and in particular your month abbreviations are not accepted
Marz = char([77 228 114 122]); %'März' -- but protect in case your system only uses 7 bit characters for .m files
date_strings = regexprep(date_strings, {'Mrz', 'Jun(?=\W)', 'Jul(?=\W)'}, {Marz, 'Juni', 'Juli'}, 'ignorecase');
dates = datetime(date_strings,'InputFormat', 'eee MMM d HH:mm:ss yyyy', 'locale', 'de_DE');
%plot
plot(dates, voltages);
Your sample input file only happened to have two different month abbreviations, one of which was not ISO standard but appears to be somewhat common. I speculate that you might have 'Jun' and 'Jul' in your dates so I went ahead and translate those to Juni and Juli for processing.