Set tick label of the color bar with dates in the format yyyy-MM-dd HH:mm:ss.

39 views (last 30 days)
Dear all,
I have a dataset (x,y,z) that I am plotting over time. The time column was originally in the following format 'yyyy-mm-dd HH:mm:ss'.
For the plot, I converted it to decimal year, and this is the label that appear in the colorbar. But I would like to have the colorbar label in the format 'yyyy-mm-dd HH:mm:ss'.
I tried something similar to this:
hcb.TickLabels = datestr(dates, 'yyyy-MM-dd HH:mm:ss');
but the label are not the corrected one (see figure).
Has someone a suggestions?
Thanks to all the will reply,
Chiara
  4 Comments
Steven Lord
Steven Lord on 11 Dec 2024 at 16:15
You used "mm" twice in the format, and that is interpreted as the minutes data. If you were using datetime MATLAB should have warned you about this as shown below. You want to use MM or MMM for months.
dt = datetime('now')
dt = datetime
11-Dec-2024 16:14:53
F = dt.Format
F = 'dd-MMM-uuuu HH:mm:ss'
dt.Format = replace(F, 'MMM', 'MM') % 2-digit month instead of short name
dt = datetime
11-12-2024 16:14:53
dt.Format = replace(F, 'MMM', 'mm') % minutes in place of months
Warning: The format 'dd-mm-uuuu HH:mm:ss' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
dt = datetime
11-14-2024 16:14:53
Chiara Lanzi
Chiara Lanzi on 12 Dec 2024 at 9:10
Dear Steven Lord,
Thanks for pointing this out, I have to admit I did not see hta tI was using twice "mm", my bad.
Chiara

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 11 Dec 2024 at 15:56
I'm not sure how you created the dates variable used to set the tick labels. It can be difficult to convert decimal years back into a date string since you often lose the precision needed to accurately reconstruct the datetime.
Here's an example of how you could avoid that by manually creating the colorbar ticks and labels.
Z = peaks;
times = datetime('now','Format','yyyy-MM-dd HH:mm:ss') + days(Z);
dyears = decyear(times)
dyears = 49×49
1.0e+03 * 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249 2.0249
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
surf(dyears)
hcb = colorbar;
% manually set the colorbar tick locations and labels
start = min(times(:));
stop = max(times(:));
dticks = linspace(start,stop,10);
hcb.Ticks = decyear(dticks);
hcb.TickLabels = string(dticks);
ax = gca;
ax.OuterPosition(3) = 0.8;
  1 Comment
Chiara Lanzi
Chiara Lanzi on 12 Dec 2024 at 9:08
Dear Cris LaPierre,
thanks for your time in answering my question. I foolowe your suggestions and it worked quite well. Thnak you for your help.
Chiara

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!