Increasing ticks on x-axis when using datenums?

8 ビュー (過去 30 日間)
Cmullica
Cmullica 2020 年 7 月 28 日
コメント済み: Cmullica 2020 年 7 月 28 日
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!

採用された回答

Cris LaPierre
Cris LaPierre 2020 年 7 月 28 日
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 件のコメント
Cmullica
Cmullica 2020 年 7 月 28 日
This did the trick. Thank you kindly!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by