Increasing ticks on x-axis when using datenums?

2 vues (au cours des 30 derniers jours)
Cmullica
Cmullica le 28 Juil 2020
Commenté : Cmullica le 28 Juil 2020
Hi all, question is exactly as the title says. First time working with datenums so I'm kind of flying by the seat of my pants on this. I'll include the entirety of my code since it's short enough.
% Read from .csv file
data = readtable('filename.csv', 'HeaderLines',2);
%%
% parsing the table
time = data(:,1);
real = data(:,2);
reactive = data(:,3);
% serializing time and converting tables to arrays
time = table2array(time); % Format: yyyy-mm-dd HH:mm:ss
real = table2array(real); % [
reactive = table2array(reactive);
datenumber = datenum(time);
type = class(datenumber)
plot(datenumber,real)
datetick('x', 'yyyy-mm-dd')
xlabel('\sl\bfDate')
ylabel('\sl\bfReal Power [kW]')
title('\sl\bfReal Power vs. Time')
% XTicks = [datenum(2015,1,1):datenum(2020,1,1)]
% XTickLabels = datestr(XTicks,6);
% set(gca,'XTick',XTicks,'XTickLabel',XTickLabels)
You can see that the last three lines are me experimenting with how to increase the x-ticks, but it's if I utilize those lines, a solid black box appears where I would typically have values on the x-axis.
Here is the output:
Ultimately, I aim to have ticks at each half year mark between 2015 and 2020.
Any help would be appreciated!

Réponse acceptée

Cris LaPierre
Cris LaPierre le 28 Juil 2020
This is handled automatically for you if you use datetimes. Use the function dateime to convert your time to a datetime. Then plot that as x instead of datenum. Format the appearance using xtickformat.
  1 commentaire
Cmullica
Cmullica le 28 Juil 2020
This did the trick. Thank you kindly!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time 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!

Translated by