MATLAB Answers

Import dates from csv files and plot them

16 views (last 30 days)
Hello there, I am using Matlab 2019a and use app designer and I have problem with importing dates from csv file and plot them from uitable. This is my code
First Button
[filename,path] = uigetfile(...
{'*.csv', 'Text file CSV (*.csv)'}, 'Load File', 'MultiSelect', 'on');
onefile = readtable(filename, 'Delimiter', '\t ;', 'MultipleDelimsAsOne', true);
app.uitable1.Data = onefile;
setappdata(0,'table',onefile);
Second Button
T = getappdata(0, 'table');
A = table2cell(T);
ydata1 = cellfun(@mean,A(:,3));
ydata2 = cellfun(@mean,A(:,7));
Times = cellfun(@mean,A(:,2));
str = convertCharsToStrings(A(:,1));
DateString = datestr(str);
DateNumber = datenum(DateString);
Dates = datetime(DateNumber, 'convertFrom', 'datenum','Format', 'dd.MM.yy HH:mm');
xdata = Dates + Times;
plot(app.axes1,xdata,ydata2);
It is doing that that convert the format and make the date March 2nd to February 3rd. I also tried table2timetable but it thinks that the column with dates is text so it doesnot work. So I have table which is in cell array and contains character. How can solve this please?I tried almost everything and look through this FAQs, but I still doesnot found that will helps me.

  3 Comments

Johannes Hougaard
Johannes Hougaard on 15 Apr 2020
Hi Sarlota
As far as I can tell from your question, the problem arises in the lines
DateString = datestr(str);
DateNumber = datenum(DateString);
As the conversion to DateString is unnecessary and does not hold the right options.
My solution would be to skip the conversion to a datestr and jump right onto the datenum, while setting the FormatIn for the datenum function to match your date format from the .csv
It seems that the correct line is:
DateNumber = datenum(str,'dd-mm-yyyy');
and just keeping the remaining code.
Sarlota Duskova
Sarlota Duskova on 15 Apr 2020
Hello,
Thank you for your quick reply, I also skipped the line with str and take data from table and now it is work.
So I thank you for your help. :)
Johannes Hougaard
Johannes Hougaard on 16 Apr 2020
You're welcome.
I'll post my comment to the 'Answer' section as unfortunately I filled in the wrong place.
If you accept the answer afterwards we'll help clear the backlog of unanswered questions.

Sign in to comment.

Accepted Answer

Johannes Hougaard
Johannes Hougaard on 16 Apr 2020
Hi Sarlota
As far as I can tell from your question, the problem arises in the lines
DateString = datestr(str);
DateNumber = datenum(DateString);
As the conversion to DateString is unnecessary and does not hold the right options.
My solution would be to skip the conversion to a datestr and jump right onto the datenum, while setting the FormatIn for the datenum function to match your date format from the .csv
It seems that the correct line is:
DateNumber = datenum(str,'dd-mm-yyyy');
and just keeping the remaining code.

  0 Comments

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 27 Apr 2020
Sarlota, I recommend that you use datetime, and not use datenum or datestr at all. Especially do not do this
DateString = datestr(str);
DateNumber = datenum(DateString);
Dates = datetime(DateNumber, 'convertFrom', 'datenum','Format', 'dd.MM.yy HH:mm');
Just convert the text to datetime directly.
But there are some very funny things in your code and your file. You are using multiple delimiters in a file that is clearly semicolon delimited. And the file appears to have either one too many headers, or one too few data columns. And the date/time format you are using does not math what's i the file. You code is very complicated as a result - I think you are making you life too difficult. Let's assume the file is missing a column. I added a semicolon at the end of each data line, equivalent to having an empty field for the last column n the file. (Or maybe I should have deleted the 'current' in the header; I can't tell.) In your MATLAB release, try this
>> T = readtable('matlab.csv', 'Delimiter', ';')
T =
7×9 table
time current avg min max spctemp wlencomp ledpower event
______________________________ _______ _______ _______ _______ _______ ________ ________ _____
{'02-03-2020 14:25:25.468972'} -92.368 -92.33 -92.382 -92.266 24.56 0 25000 NaN
{'02-03-2020 14:25:29.470035'} -92.325 -92.334 -92.398 -92.279 24.56 0 25000 NaN
{'02-03-2020 14:25:33.467210'} -92.361 -92.338 -92.379 -92.279 24.56 0 25000 NaN
{'02-03-2020 14:25:37.477145'} -92.339 -92.354 -92.398 -92.296 24.56 0 25000 NaN
{'02-03-2020 14:25:41.475953'} -92.348 -92.38 -92.437 -92.331 24.56 0 25000 NaN
{'02-03-2020 14:25:45.478137'} -92.36 -92.398 -92.454 -92.347 24.56 0 25000 NaN
{'02-03-2020 14:25:49.475570'} -92.425 -92.386 -92.434 -92.333 24.56 0 25000 NaN
>> T.time = datetime(T.time,'Format','dd-MM-uuuu HH:mm:ss.SSSSSS')
T =
7×9 table
time current avg min max spctemp wlencomp ledpower event
__________________________ _______ _______ _______ _______ _______ ________ ________ _____
02-03-2020 14:25:25.468972 -92.368 -92.33 -92.382 -92.266 24.56 0 25000 NaN
02-03-2020 14:25:29.470035 -92.325 -92.334 -92.398 -92.279 24.56 0 25000 NaN
02-03-2020 14:25:33.467210 -92.361 -92.338 -92.379 -92.279 24.56 0 25000 NaN
02-03-2020 14:25:37.477145 -92.339 -92.354 -92.398 -92.296 24.56 0 25000 NaN
02-03-2020 14:25:41.475953 -92.348 -92.38 -92.437 -92.331 24.56 0 25000 NaN
02-03-2020 14:25:45.478137 -92.36 -92.398 -92.454 -92.347 24.56 0 25000 NaN
02-03-2020 14:25:49.475570 -92.425 -92.386 -92.434 -92.333 24.56 0 25000 NaN
>> TT = table2timetable(T)
TT =
7×8 timetable
time current avg min max spctemp wlencomp ledpower event
__________________________ _______ _______ _______ _______ _______ ________ ________ _____
02-03-2020 14:25:25.468972 -92.368 -92.33 -92.382 -92.266 24.56 0 25000 NaN
02-03-2020 14:25:29.470035 -92.325 -92.334 -92.398 -92.279 24.56 0 25000 NaN
02-03-2020 14:25:33.467210 -92.361 -92.338 -92.379 -92.279 24.56 0 25000 NaN
02-03-2020 14:25:37.477145 -92.339 -92.354 -92.398 -92.296 24.56 0 25000 NaN
02-03-2020 14:25:41.475953 -92.348 -92.38 -92.437 -92.331 24.56 0 25000 NaN
02-03-2020 14:25:45.478137 -92.36 -92.398 -92.454 -92.347 24.56 0 25000 NaN
02-03-2020 14:25:49.475570 -92.425 -92.386 -92.434 -92.333 24.56 0 25000 NaN
Your code does not seem to match what's in your file at all; all those lines involving cellfun in particular seem to not make any sense when I run it. But I would think that all you need at this point is
>> plot(TT.time,TT.current)

  1 Comment

Sarlota Duskova
Sarlota Duskova on 2 May 2020
Hello, I used this delimiter '\t ;' because I had error and error sayed I must used that delimiter and when I used it, error disappeared. Then I did not know what to do with date and time so I converted table into cell so that is the reason why I used cellfun. I am a beginner so I need to learn a lot and I am trying some stuff you can see it above and then it works but yeah your sollution is much more easier I dont know why I dont do that before. I spent a lot of time to figure out how to do it, and I did my stupid sollution you can see above. So thank you that you looked at it and help me to see that the solution it can be much more easier.

Sign in to comment.

Products


Release

R2019a

Translated by