Borrar filtros
Borrar filtros

Imagesc with datetime on x axis

17 visualizaciones (últimos 30 días)
Siegmund
Siegmund el 13 de Sept. de 2022
Comentada: dpb el 15 de Sept. de 2022
I'd like to use imagesc to plot a matrix with distance on y axis, z in colorbar, and datetime on x axis. Y and Z are in the matrix (12 x 500) and datetime is in seperate vector t (12 x 1).
I have the following code but the datetime that shows up on the x axis is January 1, January 2, ... (from 1 - 12).
If I add t, the imagesc doesn't show up.
How can I plot the 12 x 500 matrix and plot t as x axis?
figure
imagesc(D', 'Interpolation', 'bilinear');
set(gca, 'YDir', 'normal');
colorbar
set(gca, 'XTick', t);
datetick('x', 'mmmm dd HH:MM', 'keepticks')
  1 comentario
Siegmund
Siegmund el 13 de Sept. de 2022
In addition, any suggestion on how to extent the interval between the ticks? t shows intervals of 15min. It would be nice if it would only show the day and with a time interval of 6 hours...

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Sept. de 2022
Unfortunately, it is not supported to use datatime() objects for imagesc axes.
datetick() is not for datetime() objects: it is for the older serial date numbers, which is numerically days and fractions of a day since a particular starting point.
If your t is datetime() objects then use
tnum = datenum(t);
imagesc(D', 'Interpolation', 'bilinear', 'XData', tnum);
set(gca, 'YDir', 'normal')
colorbar
xticks(tnum);
datetick('x', 'mmmm dd HH:MM', 'keepticks')
  8 comentarios
Siegmund
Siegmund el 15 de Sept. de 2022
Editada: Siegmund el 15 de Sept. de 2022
If I'd add the missing timesteps in the matrix as NaN, the plot would look like we wanted?
I tried the following but that's not entirely working yet.
Times = datetime(t(:),'ConvertFrom','datenum');
WP = timetable(Times, D);
tnum = retime(WP, 'regular', 'mean', 'TimeStep', minutes(15));
N = height(tnum);
for i = 1:N
if any(isnan(tnum(:,i)))
D(i,:)=NaN;
end
end
imagesc(D', 'Interpolation', 'bilinear', 'XData', tnum);
set(gca, 'YDir', 'normal')
colorbar
xticks(tnum);
datetick('x', 'mmmm dd HH:MM', 'keepticks')
dpb
dpb el 15 de Sept. de 2022
...
N = height(tnum);
for i = 1:N
if any(isnan(tnum(:,i)))
D(i,:)=NaN;
end
end
...
in MATLAB is simply written as
D(any(isnan(tnum(:,i))),:)=NaN;
using vectorized logical addressing.
But, you don't need to do that, anyways, if either x or y variable is not finite the HG2 routines won't show anything for those locations so simply the bad x value will be sufficient.
However, once you created the timetable, the time variable is a datetime and so it will be NAT and you'll need isnat instead of isnan
BUT, there's still the problem that visually this will just create bands on the axes in those locations; it won't collapse the axis; it will still be continuous over the full time span.
It takes one of the ways of adjusting the axes or the times themselves to create the effect desired. I've not tried the FEX submission linked to, in the past the one I tried had some warts; don't recall if the same or not; even if was those may have been fixed by now so give it a shot as the simplest route forward until, at least, it's shown not to be adequate.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by