Using datetick to label x axis: but don't show the last tick label - matlab

Does anyone know how to remove the last ticklabel on a plot in Matlab AFTER using the datetick function to put the labels there?
I am plotting Y data and X dates (years and months converted to a datenum).
Then I am using the following to plot the year labels on the xaxis:
close all;clear all;clc;
[num,txt,raw] = xlsread('data.xlsx');
yr = num(:,1);
mth= num(:,2);
data= num(:,3);
dates=datenum(yr,mth,1);
plot(dates,data,'r-.','linewidth',2);
dateFormat = 10;
datetick('x',dateFormat)
I would like to remove the last tick label, as it is including a year that isn't in the datset (presumably Matlab is optimising the distance between ticks and interpolating to the next year).

You should set the ticks you want on your chart first, e.g.:
set(gca, 'XTick', x_values_you_want_ticks_at);
Then use datetick with 'keepticks' option, which will preserve your ticks location:
datetick(gca, 'x', dateFormat, 'keepticks');

Related

Plotting a set of data with time in hh:mm format in x axis in matlab

I have a code which goes like this:
clc;clear;close all
%%
Time=linspace(16.8,17.8,230400)';
Field=linspace(50,145,230400)';
figure('units','normalized','outerposition',[0 0 1 1])
plot(Time,Field)
%%
figure('units','normalized','outerposition',[0 0 1 1])
hammng_wndw_size=4096;
window=hamming(hammng_wndw_size); %window size
noverlap=512; % the noverlaps its the no. of points for repeating the window
nfft=4096; %size of fft
fs=32; %sampling freq
[Sp,F,T,P]=spectrogram(Field,window,noverlap,nfft,fs,'yaxis');
T_forspectrogrm=T./3600+Time(1);
surf(T_forspectrogrm,F,10*log10(P),'edgecolor','none','FaceColor','interp');
axis tight;ylim([0 4]);view(0,90);
colormap(jet);colorbar;
The result of this plot is these two figures:
The xaxis is time and y axis is some other quantity, lets say field. Now, when I plotting, the x axis starts from 16.8 to 17.8 for first figure and 16.8 and 18.8 for second figure. This actually corresponds to 16:48:00 to 17:48:00 and similarly for the second one. How do I have to modify the program to convert the x-axis into hh:mm format do this job?
I tried in this fashion
TimeInReqFrmat=datestr(Time(:,1),'HH:MM:SS'), but this gives me a string of characters.
I am using Matlab 2016a.
Thanks in advance
You approach was almost right. You need to use datetimes. It's just a bit tricky to convert decimal numbers to date times
x = [16.8;17.8]
H = floor(x); % hour
m = floor((x-H)*60); % minute
S = (x-H)*60-m; % second
% create date vector
DateVec = datetime(0,0,0,H,m,S);
plot(DateVec,rand(size(DateVec)))
% set the tick format
xtickformat('HH:mm')
There are many options to set the xtickformat.
If you have unix-time, you can convert it right away
datetime(x,'ConvertFrom','datenum')
If you have just time, you will need to come up with a date for a proper datetime. Otherwiese you can also think of using duration.

MATLAB: How to change x-axis scale with dateticks

I try to plot unemployment over time but years are displayed in 10 year frequency. I want to have x-axis ticks every 5 years but can't seem to manage this as xticks are not in dateformat
Below is the relevant code if you want to take a look.
clear;
X=xlsread('UNRATE.xls');%Import Data
s_year=1950;
e_year=2015;
X=X((s_year-1948)*12+1:(e_year-1948)*12+12);
startdate = datenum('01-1950','mm-yyyy');
enddate = datenum('12-2015','mm-yyyy');
dt = linspace(startdate,enddate,792);%create numeric dates
plot(dt,X);%plot data against dates
datetick('x','yyyy');%format dates in display
recessionplot;
title('Unemployment Rate in the US over time');
xlabel('Year'); % x-axis label
ylabel('Unemployment Rate'); % y-axis label
When working with time series, the best you can do is to manually modify the XTick property of the axis so that it fits your needs once the plotting has been performed. For example:
%...
plot(dt,X);
set(gca,'XTick',datenum(1950:5:2015,1,1));
%...
Working example:
clear;
s_year=1950;
e_year=2015;
X= rand(792,1);
startdate = datenum('01-1950','mm-yyyy');
enddate = datenum('12-2015','mm-yyyy');
dt = linspace(startdate,enddate,792);
xticks = datenum(1950:5:2015,1,1);
xlabels = cellstr(num2str(year(xticks.')));
figure();
plot(dt,X);
set(gca,'XTick',xticks);
set(gca,'XTickLabel',xlabels);
Output:
Original plot without ticks adjustement:

How to set xticklables to certain places in the graph in matlab

I have two vectors that contain the average temperatures of two cities. I am plotting those vectors to a graph. I have 398 temperatures in both vectors so my xticklables are naturally from 1 to 398. I am trying to change my xticklables so they would show the month and the year like this: 01/2017. however I seem to be unable to write anything to my xtick. Here is my code for xtickmodification:
ax.XTick=[1,32,62,93,124,154,185,215,246,277,305,336,366,397];
ax.XTicklabels={'05/2014','06/2014','07/2014','08/2014','09/2014',
'10/2014','11/2014','12/2014','01/2015','02/2015','03/2015','04/2015','05/2015','06/2015'};
ax.XTick contains the vector element that starts a new month. So how can i get the ax.XTicklabels-line to my graph?
use XTickLabel property instead of XTicklabels. in addition, you can set XTickLabelRotation property if the labels are long strings:
x = rand(398,1);
h = plot(x);
ax = gca;
ax.XTick=[1,32,62,93,124,154,185,215,246,277,305,336,366,397];
ax.XTickLabel={'05/2014','06/2014','07/2014','08/2014','09/2014',...
'10/2014','11/2014','12/2014','01/2015','02/2015','03/2015','04/2015','05/2015','06/2015'};
ax.XTickLabelRotation = 45;
Have you tried the following
set(gca, 'xtick', ax.XTick);
set(gca, 'xticklabels', ax.XTicklabels);

Bar Graph change x axis from numbers to date in matlab

I want to ask how I could change numbers 1,2,3,4... from graph, I want dates (1 -> 11_08_2016_12_36 without .mat) in graph from variables in workspace fnames.name. I tried some function handle but it does not work. Thank you for help.
Code
N={fnames.name}; %isolate the names
t=zeros(1,length(N)); %reserve space
for ct = 1:length(N) %go over the names
temp=regexp(N{ct},'\d\d_\d\d_\d\d\d\d_\d\d_\d\d','match'); %match the relevant part
t(ct)=datenum([temp{:}],'mm_dd_yyyy_HH_MM'); %convert to datenum
end
%plot your graph here, use t as the x-axis%
datetick('x',1) %give x-axis in date
Use the axis handles.
data=rand(3,1);
hfig = figure;
hax = axes;
hbar = bar(1:3,data);
dates={'Date 1';'Date 2';'Date 3'};
hax.XTickLabel=dates;
% In case you want your labels rotated.
hax.XTickLabelRotation=90;

Create and Plot Time Series Data in Matlab

I have a (1x700) vector x for which I would like to create and plot a time series object in Matlab. Each observation corresponds to one month, and the first observation belongs to January 1960. I tried the following:
state1 = timeseries(x,1:size(x,2));
state1.Name = 'Test';
state1.TimeInfo.Units = 'months';
state1.TimeInfo.StartDate = 'Jan-1960'; % Set start date.
state1.TimeInfo.Format = 'yy'; % Set format for display on x-axis.
state1.Time = state1.time - state1.time(1); % Express time relative to the start date.
plot(state1);
However, I still see numbers on the x-axis instead of years. Could anyone please help? Thanks in advance!
Create random data. 1/12 corresponds to the fraction of a year that each month represents.
x = 1960:1/12:1970;
y = rand(1,121);
Then plot the x and y axes data using plot.
plot( x, y )
Then set the tick as follows for a decade per year. 1960:1970 will generate [1960 1961 ...] each corresponding to the tick's year.
set( gca, 'XTick', 1960:1970 );
Here is the output plot.
Doing 1 year intervals get VERY MESSY with lots of data. So solutions include doing a larger interval or setting your ticks to display vertically instead of horizontally. This code below shows how to set 5 year intervals instead.
set( gca, 'XTick', 1960:5:2010 );