How can I plot dates?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Márcio Gonçalves
le 11 Avr 2020
Modifié(e) : Cris LaPierre
le 27 Avr 2020
Hello,
How can I plot date in x-axis?
My csv file import dates as: '03/27/2020 19:10:35.537'
My code:
D = data{:,2};
d = datenum(D);
date = datetime(d,'Format','dd-MM-yyyy HH:mm:ss.SS','convertFrom','datenum');
As a result, I have my plots good, but only in same data (see Figure).
All my data has the same time, and have the same format 805 x 1 (double). For example, yellow line(good) and blue line (wrong) should be the same on x-axis.

0 commentaires
Réponse acceptée
Cris LaPierre
le 11 Avr 2020
I think we need to see your plot command to say for certain what is happening.
My recommendation would be to have your X data be the datetime variable. MATLAB is able to use that as X, and will may the XTickLabels the dates, which are easier to understand.
I would therefore use readtable to load the csv file. That way, I can import the dates directly as datetimes.
8 commentaires
Peter Perkins
le 14 Avr 2020
Don't use the datetick function unless you have a veru old version of MATLAB. Stay away from datenum, datestr, and datetick. In fact, no offense to earlier responders intended, don't do any of that.
In a recent version of MATLAB, here's all you need:
>> tt = readtimetable('test1.csv','RowTimes','Time','PreserveVariableNames',true);
>> head(tt)
ans =
8×61 timetable
Time Sweep # Chan 101 (C) Chan 102 (C) Chan 103 (C) Chan 104 (C) Chan 105 (C) Chan 106 (C) Chan 107 (C) Chan 108 (C) Chan 109 (C) Chan 110 (C) Chan 111 (C) Chan 112 (C) Chan 113 (C) Chan 114 (C) Chan 115 (C) Chan 116 (C) Chan 117 (C) Chan 118 (C) Chan 119 (C) Chan 120 (C) Chan 201 (C) Chan 202 (C) Chan 203 (C) Chan 204 (C) Chan 205 (C) Chan 206 (C) Chan 207 (C) Chan 208 (C) Chan 209 (C) Chan 210 (C) Chan 211 (C) Chan 212 (C) Chan 213 (C) Chan 214 (C) Chan 215 (C) Chan 216 (C) Chan 217 (C) Chan 218 (C) Chan 219 (C) Chan 220 (C) Chan 301 (VDC) Chan 302 (VDC) Chan 303 (VDC) Chan 304 (VDC) Chan 305 (VDC) Chan 306 (VDC) Chan 307 (VDC) Chan 308 (VDC) Chan 309 (VDC) Chan 310 (VDC) Chan 311 (VDC) Chan 312 (VDC) Chan 313 (VDC) Chan 314 (VDC) Chan 315 (VDC) Chan 316 (VDC) Chan 317 (VDC) Chan 318 (VDC) Chan 319 (C) Chan 320 (C)
_______________________ _______ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ____________ ____________
03/27/2020 19:10:35.537 1 15.4 15.4 15.3 15.2 16 15.5 15.4 15.3 15.3 15.3 20.3 20.8 20.5 21 20.1 20.2 20.2 20.3 19.1 19 18.3 19.3 18.2 19.5 17.8 18.6 23 15.8 15.7 20.5 21.1 20.8 20.4 22.6 20.5 20.5 19.7 21.1 19.6 16.2 20.5 73.1 -24.3 99.6 20 74.9 19.9 82.7 20 81.3 15.9 100 4.34e-05 4.07e-05 -3.79e-05 -4e-05 -3.97e-05 -5.59e-05 18 18.6
03/27/2020 19:15:35.536 2 15.4 15.4 15.3 15.2 16 15.5 15.4 15.3 15.4 15.3 21 21.4 21.1 21.8 20.8 20.9 20.9 20.9 19.7 19.6 18.8 20 18.7 20.2 18.3 19.2 23.3 15.8 15.7 21.1 21.7 21.5 21 23.2 21 21.1 20.4 21.7 20.2 16.1 21.2 72.8 -24.3 99.6 20.7 74.5 20.4 82.3 20.6 80.8 15.9 100 4.22e-05 3.76e-05 -3.97e-05 -3.71e-05 -4.02e-05 -5.49e-05 18.3 19
03/27/2020 19:20:35.536 3 15.4 15.4 15.3 15.2 16 15.5 15.4 15.3 15.4 15.3 21.4 21.9 21.6 22.3 21.3 21.3 21.3 21.4 20.2 20.2 19.2 20.5 19.1 20.7 18.7 19.8 23.4 15.8 15.7 21.6 22.2 21.9 21.5 22.4 21.5 21.6 22.5 22.1 20.7 16.2 21.7 72.5 -24.3 99.6 21.2 74.4 21 81.9 21.2 80.3 15.9 100 4.25e-05 3.23e-05 -3.83e-05 -3.29e-05 -3.88e-05 -5.12e-05 18.6 19.2
03/27/2020 19:25:35.536 4 15.4 15.4 15.4 15.2 16 15.5 15.4 15.3 15.4 15.3 21.7 22.1 21.9 22.4 21.6 21.6 21.6 21.8 20.6 20.6 19.5 21 19.4 21.1 19 20.1 23.4 15.8 15.7 21.9 22.4 22.1 21.8 22.4 21.8 22 22.7 22.3 21 16.1 22 72.3 -24.2 99.6 21.5 74 21.4 81.5 21.5 79.9 15.9 100 3.88e-05 2.65e-05 -3.73e-05 -2.78e-05 -3.72e-05 -4.77e-05 18.9 19.3
03/27/2020 19:30:35.576 5 15.4 15.5 15.4 15.2 16 15.5 15.5 15.3 15.5 15.4 21.9 22.3 22 22.5 21.9 21.8 21.8 22 20.9 20.9 19.7 21.3 19.6 21.3 19.2 20.4 23.3 15.8 15.7 22.1 22.6 22.3 22.1 22.5 22 22.2 22.7 22.5 21.3 16.2 22.3 72.1 -24.1 99.5 21.8 73.9 21.6 81.2 21.8 79.7 15.9 100 3.77e-05 2.31e-05 -3.55e-05 -2.33e-05 -3.66e-05 -4.46e-05 19.1 19.5
03/27/2020 19:35:35.574 6 15.5 15.5 15.4 15.2 16 15.6 15.5 15.3 15.5 15.4 22.1 22.5 22.1 22.6 22.1 22 21.9 22.2 21.1 21.1 19.9 21.5 19.7 21.5 19.4 20.5 23.5 15.8 15.7 22.3 22.7 22.4 22.2 22.4 22.2 22.3 22.8 22.5 21.5 16.2 22.5 72 -24.1 99.5 22 73.8 21.8 81 21.9 79.5 16 100 3.53e-05 1.93e-05 -3.59e-05 -2.12e-05 -3.64e-05 -4.28e-05 19.2 19.6
03/27/2020 19:40:35.536 7 15.5 15.5 15.4 15.2 15.5 15.6 15.5 15.3 15.5 15.4 22.2 22.6 22.2 22.6 22.2 22 22 22.3 21.2 21.2 20 21.7 19.8 21.6 19.5 20.7 23.3 15.8 15.7 22.4 22.8 22.5 22.4 22.4 22.3 22.5 22.8 22.6 21.7 16.1 22.7 72 -24.1 99.4 22.2 73.7 21.9 80.7 22.1 79.3 16 100 3.47e-05 1.67e-05 -3.5e-05 -1.91e-05 -3.51e-05 -4.15e-05 19.2 19.6
03/27/2020 19:45:35.546 8 15.5 15.5 15.5 15.2 15.5 15.6 15.5 15.4 15.5 15.5 22.2 22.6 22.2 22.7 22.3 22.2 22.2 22.4 21.3 21.4 20.1 21.8 19.9 21.8 19.6 20.8 23.4 15.8 15.7 22.5 22.9 22.6 22.5 22.5 22.4 22.6 22.9 22.7 21.8 16.1 22.7 71.9 -24.1 99.5 22.3 73.6 22.1 80.6 22.2 79.2 16 100 3.52e-05 1.66e-05 -3.57e-05 -1.85e-05 -3.65e-05 -4.13e-05 19.3 19.6
>> stackedplot(tt(:,1:10))

In less recent versions, you won't be able to use PreserveVariableNames, they will be modified with a warning. And you won't be able to use readtimetiable, use readtable and table2timetable.
If stackedplot isn't doing what you need, you can make whatever other plot you want with something like
plot(tt.Time,tt.Chan101_C_)
but don't use datetick on a datetime plot. For sure, don't do this:
D = data{:,2};
d = datenum(D);
date = datetime(d,'Format','dd-MM-yyyy HH:mm:ss.SS','convertFrom','datenum');
If the data are read in as text, convert them directly to datetime. In recent versions, read them in directly as datetimes using detectimportoptions.
Plus de réponses (1)
Márcio Gonçalves
le 20 Avr 2020
2 commentaires
Peter Perkins
le 27 Avr 2020
Modifié(e) : Peter Perkins
le 27 Avr 2020
As I said, "In less recent versions, you won't be able to use PreserveVariableNames, they will be modified with a warning. And you won't be able to use readtimetiable, use readtable and table2timetable." Unless you are using a really old version of MATLAB, I really, strongly, recommend that you try what I have suggested.
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!