## How to change values of x axis from numbers to dates

### Leo Felix (view profile)

on 10 Apr 2019
Latest activity Commented on by dpb

on 10 Apr 2019

### dpb (view profile)

I am given a complex graphic in MATLAB where currently the values on the x axis are given in numbers from 0-800. I would like to change these values into an arbitrary range of dates on the current plot.
As an example consider the following toy problem. I plot a sine curve in the interval from -10 to 10:
x = -10:0.1:10;
y = sin(x);
fig = figure;
plot(x,y)
Now I would like to change the values on the x axis to show dates, say 01-01-2019 to 11-01-2019 (dd-MM-yyyy), i.e. the interval -10 to 10 should correspond to the time range 01-01-2019 to 11-01-2019. And I would like to do this without having to plot this function again.
The reason for this is because my orginal graphic is much more complex and so I am unable to simply do something like
t = datetime(2019,01,01) + hours(0:24*10);
x = linspace(-10,10,length(t));
plot(t,sin(x))
which would fix the toy problem. Instead I need to somehow change the properties of the existing figure in some way.
While searching for a solution for this problem I have come accross the functions xtickformat, xticks, xlabels and the likes, but unfortunately I am still rather clueless.
Any help is much appreciated, thanks in advance!

### dpb (view profile)

on 10 Apr 2019

x = -10:0.1:10;
y = sin(x);
hF=figure; % keep handle to figure
plot(x,y)
hAx(1)=gca; % and the first axes
t = datetime(2019,01,01) + hours(0:24*10);
hAx(2)=axes('Position',hAx(1).Position,'color','none','Parent',hF); % make second axes for time
set(get(hAx(1),'XRuler'),'visible','off') % hide the x axis for original
set(get(hAx(2),'YRuler'),'visible','off') % and the y axis for time one
hL2=line(t,nan(size(t))); % and draw an invisible object with time axis

dpb

### dpb (view profile)

on 10 Apr 2019
What do you think wrong? Looks OK to me other than forgot to turn the outline box on:
hAx(2).Box='on';
There's really no difference between using NaN() and then turning the 'Visible' property off for a line with finite data--
hL2 = line(t,zeros(size(t)),'Visible','off') ;
altho NaN are silently ignored by plotting routines so the work is done behind the scenes for you while must set properties explicitly otherwise. Not a major deal, granted, but I can't see any difference whatsoever here; what is actually different with yours that I'm not seeing?
Leo Felix

### Leo Felix (view profile)

on 10 Apr 2019
If you look at the picture I attached, the values on the x axis only range from Jan 01 to Jan 02, as opposed to Jan 01 - Jan 12. Fortunately it seems to work out when I just draw a line of zeros and then make it invisible - I am not sure why though.
dpb

### dpb (view profile)

on 10 Apr 2019
Which ML release? I see I overlooked the effect but see the same xlim bounds either way here.
hAx(2).XLim=[t(1) t(end)];
I used NaN solely to not have to deal with line properties but it's no major deal; line is significant as opposed to plot because the latter resets axis properties and one then has to redo at least the 'Color','none' on the first axes to have the second not be occluded.

### Steven Lord (view profile)

on 10 Apr 2019

Are you trying to change just the labels, or are you trying to change the underlying data in the plot?
If the former, try the xticklabels function. If the dates and times you're trying to use to generate the labels is a datetime array, consider using char, string, or datestr on the datetime to generate the new labels.
If the latter, changing the data for the graphics objects from numbers to dates and times is a bigger change. While that may be possible to do in place, I'd probably first try to generate code for the figure window using the instructions in the "Generate Code to Recreate Figure" section on this documentation page (which will capture not only the code you ran to generate the graphics but also modifications you made to the figure, axes, and graphics objects using the plot tools.)
Once I had the code, I would execute it with the new datetime X data instead of the numeric data. You may need to modify some of the generated code (depending on how extensive your customizations to the figure / axes / line are) but it should give you a good starting point that's close to the appearance of the graphics created with numeric data.

Leo Felix

### Leo Felix (view profile)

on 10 Apr 2019
Hi Steven, thank you for your answer! Unfortunately I find myself in the latter case where I need to change the data for the object from numbers to dates.
Generating and modifying the original code used to plot the graphic would probably be the cleanest solution. However, I aim to do this for a large number of graphics hence I am interested in a way to modify this using only a few lines of code. Do you think it is possible to change the relevant bits of the graphic using certain commands?
dpb's solution works for me, so I will probably just use that for now. I would still be grateful if you could nevertheless share any ideas you have for a perhaps 'cleaner' solution of this matter.
dpb

### dpb (view profile)

on 10 Apr 2019
Hmmm...I thought the original problem statement indicated you didn't want to modify the original plot other than trivially...
Steven's answer is the most thorough way to regenerate the figures from the existing but it's likely one could selectively mung on the XData property values and redo the limits to effect the same appearance without regenerating the whole thing.
Think it would take having a figure available to see just what it is to be able to tell just what the chances are of just a simple scaling change of variable.