Plotting only the year of a column vector with full date

4 vues (au cours des 30 derniers jours)
Naweed Niaz
Naweed Niaz le 21 Oct 2020
Commenté : Peter Perkins le 19 Nov 2020
I have a column vector with the dates in the format "MM/dd/yyyy." I would like to plot this against another vector, but only using the year part of the date. How can I extract the yyyy part and assign it to a new vector variable to be able to plot it?
  2 commentaires
Rik
Rik le 21 Oct 2020
Do you have tyhe dates as a datetime vector, as a string vector, or as a cell vector of chars? And what did you try already?
Naweed Niaz
Naweed Niaz le 21 Oct 2020
its currently imported as a datetime vector, and I haven't tried anything because I am unsure of how to do this.

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 21 Oct 2020
Modifié(e) : Adam Danz le 23 Oct 2020
Demo:
dt = datetime(1999,01,01) + days(1:100:5000);
y = rand(size(dt));
ax = gca();
plot(dt,y)
xtickformat(ax, 'yyyy')
% or
datetick(ax, 'x', 'yyyy', 'keeplimits')
  1 commentaire
Peter Perkins
Peter Perkins le 19 Nov 2020
Don't use datetick in a datetime plot. xtickformat is the way to go.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by