MATLAB: Import dates from csv files and plot them

app designercsvdatesfileMATLABplotreadtabletime

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.

Best Answer

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.
Related Question