Convert complicated csv to matlab, multiple dilimiters and implement strings to date and time value
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Nikolai Block
el 12 de Mzo. de 2017
Comentada: Stephen23
el 12 de Mzo. de 2017
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!
0 comentarios
Respuesta aceptada
Walter Roberson
el 12 de Mzo. de 2017
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.
1 comentario
Stephen23
el 12 de Mzo. de 2017
Thank you Walter, that was a perfect answer, thank you for your help!
You have been indeed right. It's a german format. Was working before in Qt and used the class QDateTime, should have already fixed it there. But here it works anyway. Thank you!
Más respuestas (0)
Ver también
Categorías
Más información sobre Language Fundamentals en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!