Import dates from csv files and plot them
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
Johannes Hougaard
2020-4-16
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.
采纳的回答
Johannes Hougaard
2020-4-16
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 个评论
更多回答(1 个)
Peter Perkins
2020-4-27
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!