How do I change the number of axis ticks while using 'datetick' (Object Oriented Programming, Creating an Application)
33 views (last 30 days)
Show older comments
I am working on a project that takes the data for the number of COVID-19 Deaths and Cases in all countries/state/regions of the globe, and puts them into a application that shows this information graphically (complete with widgets and all) using MATLAB's App Designer, an acompanying data file, and a classdef .m file to prepare the data. The project and all the widgets work perfectly, however the way I have set up the x-axis lable seems to be in error. The final plot should look something like this:

with monthly x-axis tick lables in the form of (mm/dd). However, when I run my appliation, my output is this (data has changed slightly, so only focus on number of x-axis ticks):

with a quarterly tick seperation instead of a monthly one. I dont want to send the entire code, so I know you wont be able to run the application, but I know that all the code leading to this point is correct I am just strugling with finding a way to alter the number of axis ticks. Below is what I beleive is the necessary code for the situation, if more is needed, please dont heasitate to ask for it and I can send through a less public medium.
This code is at the end of my 'Plot Data' method section (after all of the data has been plotted
xlim(app.UIAxes,[app.xDATA(1) app.xDATA(end)]);
xlim(app.UIAxes,'manual');
app.UIAxes.YLimMode = 'auto';
app.UIAxes.YTickMode = 'auto';
datetick(app.UIAxes,'x','mm/dd',"keeplimits");
grid(app.UIAxes,'on');
set(app.UIAxes,'YTickLabel',num2str(get(app.UIAxes,'YTick').','%.f'));
hold(app.UIAxes,'off');
This is the code in my start-up function that takes the data from my classdef and initiates it.
globalDATA = DATA('Global'); % DATA import
app.CountryListBox.Items = (unique(globalDATA.COUNTRY,'stable')); % Settings for items listed in the Country list box
app.StateorRegionListBox.Items = (unique(globalDATA.STATE,'stable')); % Settings for items listed in the State/Region List box
app.cases_deaths_MATRIX = globalDATA.Cases_Deaths_GLOBAL;
app.casesVEC = app.cases_deaths_MATRIX(1,:);
app.deathsVEC = app.cases_deaths_MATRIX(2,:);
startDATE = datenum(globalDATA.DATE{1});
endDATE = datenum(globalDATA.DATE{end});
app.xDATA = linspace(startDATE,endDATE,length(globalDATA.DATE));
app.averagingWINDOW = app.AveragedofDaysSlider.Value;
app.StateorRegionListBoxValueChanged;
I appreciate any help that is able to be offered, thank you in advance for your time and assistance.
0 Comments
Accepted Answer
Dave B
on 4 Nov 2021
Cool looking app!
One strategy is to set the ticks and then use the keepticks flag:
xdata=datenum(2020,1:4,1);
ydata=rand(1,4);
ax=nexttile;plot(xdata,ydata)
nticks = 4;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
datetick(ax,'x','mm/dd',"keepticks");
ax=nexttile;plot(xdata,ydata)
nticks = 8;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
datetick(ax,'x','mm/dd',"keepticks");
ax=nexttile;plot(xdata,ydata)
nticks = 12;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
datetick(ax,'x','mm/dd',"keepticks");
But if you can use datetime instead of datenum, you might find that options are more flexible (with xtickformat) and you get better ticks:
figure
xdata=datetime(xdata,'ConvertFrom','datenum');
ax=nexttile;plot(xdata,ydata)
nticks = 4;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
ax=nexttile;plot(xdata,ydata)
nticks = 8;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
ax=nexttile;plot(xdata,ydata)
nticks = 12;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
ax=nexttile;plot(xdata,ydata)
nticks = 12;
xticks(ax,linspace(xdata(1),xdata(end),nticks))
xtickformat('MMM dd')
11 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




