Adding date tick marks to a Matlab plot - matlab

I have a plot of time series data, and I would like to replace the tick marks of the x-axis (automatically I have the number of the ordered observations) with the date when the value is observed. I would like to have a tick mark every 5 years for example. I know how to do it with R, but with MATLAB seems so complicated and I'm not getting the result I want.

There is an answer on the Mathworks website that I think you will find helpful: http://www.mathworks.com/matlabcentral/answers/92565-how-do-i-control-axis-tick-labels-limits-and-axes-tick-locations. Basically what you want to do is manipulate the XTick or XTickLabel attributes of the current axis handle. Lets say I have a plot that spans 100 years from 1900 - 2000. After creating the plot, I can set year labels in 5 year increments by doing:
set(gca, 'XTick', 1900:5:2000);

Related

Scatter with 2 y-axes and datetick

I would really appreciate some help.
I have to independent datasets. Each dataset contains two variables: dates (as datenumber) and corresponding data. I need to plot both datasets on one scatter plot with dates on x-axis and two y-axes. I have been trying with the following code:
figure(1);
scatter(x1,y1,'g');
set(gca);
ax1=gca;
set(ax1,'YColor','g');
ax2 = axes('Position',get(gca,'Position'),'YAxisLocation','right', XTick'[],'Color','none','YColor','r');
hold on; scatter(x2,y2,'r');
Now, this gives correct y-axis on the right side, however on the right side I end up with two overlapping y-axes.
Also, I need to change x-axis so that it displays dates as opposed to datenumbers. I've tried to incorporate datetick into the code but it again gives me two overlapping x-axes.
Does anyone know how to go about it?
thank you
I tried your script with your sample input and found no problems. Anyway, here's a solution which uses the matlab function plotyy, which is suited to simple plots like this:
%generate input
x1=[732490 732509 732512 732513 732521 732528];
y1=[7.828 7.609 22.422 14.758 26.258 1.477];
x2=[732402 732403 732404 732404 732433];
y2=[0.693 0.645 0.668 0.669 0.668];
figure(1);
[ax, h1,h2]=plotyy(x1,y1,x2,y2,'scatter');
%set colors manually
green=[0 1 0];
red=[1 0 0];
set(h1,'cdata',green);
set(h2,'cdata',red);
set(ax(1),'ycolor',green);
set(ax(2),'ycolor',red);
%note the 'keepticks' and 'keeplimits' options
datetick(ax(1),'x','yyyy-mm-dd','keepticks','keeplimits');
datetick(ax(2),'x','yyyy-mm-dd','keepticks','keeplimits');
Without the datetick call the plotyy function synchronizes the xticks in the plot. When you call datetick, it recalculates the ticks, unless you explicitly tell it not to, see the option keepticks, and this is seen as two sets of x axes (even though the x coordinates are the same, the ticks are located at different positions). The keeplimits option is needed to preserve the original xlim. It obviously needs some more manual work to get plots like this sufficiently pretty.
Also note that I set the axes and data colors manually: there might be a way to do this more elegantly.
Update: keeplimits originally missing
Update2: changed sample data to correspond to updated question comment

How to plot different data in parallel (in continuation of the previous one)

I have some energy 24 hour consumption data of many days.
Plotting a specific day gives me vertical axis of consumption and horizontal axis of time.
I would like to plot for lets say 1 year.
If I use "hold on/off" command, it plots all days together on top of each other.
How can i plot in a way that for the second day, the plots goes to the continue of the first plot (horizontal axis extends automatically)? So, when I have the complete plot, it shows 365 days of energy consumption based on hour. It's like the horizontal axis is repeating while the vertical axis is updating. I'm talking about MATLAB.
You can still use hold on and plot each day separately (if I understand your question properly, this is what you want, separate plotting). Simply make sure your x-axis values are correct. So e.g. if you have one measurement value per hour, the plot day 1:
plot(1:24,valDay1,'k-')
then for day 2:
plot(25:48,valDay2,'r-')
etc. This will line things up correctly. Also, consider using a datetime as x axis values
So, I found my solution which is very simple. I don't know how it didn't occur earlier.
I just had to use ";" and that's it.
Like this:
DAY=[day1;day2;day3]
plot(DAY)

In Matlab: How to keep all xTicks but xTicklabels just on every 6 xTicks?

I have created a graph with data from an Excel file. I need to keep all the xticks but the xticklabels to appear just on every 6 ticks. I have tried doing this:
tickStep=6;
Sheet=2;
filename='MyData.xlsx';
[~,xAxis]=xlsread(filename,Sheet,'A2:A60');
yAxis=xlsread(filename,Sheet,'B2:B60');
plot(1:numel(xAxis),yAxis)
set(gca,'xtick',1:numel(xAxis))
set(gca,'xticklabel',xAxis(1:tickStep:numel(xAxis)))
Unfortunately it does not work as all the xticks are plotted, but the xticklabels appear upon each xtick instead of every 6th like I was trying to achieve. I have spent quite a long time thinking about the solution:-(. I need help. Thanks.
edit: I've included an image to help answer questions below:
In relation to this post, would it also be possible that the xticks appear every 4 xticks instead of keeping all of them, at the same time that the xticklabels are plotted every 6 xticks?, not sure about this.
You still need to enter an empty string (or empty cell) for the ticks that will have no label. You can do this by replacing the last line with these three lines:
xTickLabels = cell(1,numel(xAxis)); % Empty cell array the same length as xAxis
xTickLabels(1:tickStep:numel(xAxis)) = xAxis(1:tickStep:numel(xAxis));
% Fills in only the values you want
set(gca,'XTickLabel',xTickLabels); % Update the tick labels
Edit in response to questions below...
The reason your labels appear to be offset from the ticks is because the bottom of the letters appears to be getting lined up with the tick mark. I'm guessing you want the text justified such that the center of each letter lines up with the tick mark. There's no way to do this with the standard MATLAB axes, but there are some submissions on the MathWorks File Exchange that convert the tick labels to text objects and thus give you more options for customizing the text properties:
XTICKLABEL_ROTATE by Brian Katz
Format Tick Labels by Alexander Hayes
Once you convert the labels to text objects, you can then adjust the rotation or vertical justification by modifying the 'Rotation' and 'VerticalAlignment' properties, respectively.

Sliding Window x-axis

I already do sliding window in order to calculate some parameters of networks. In this case, I'm working with a financial network based in daily returns of 140 companies.
I already have all my calculations, but when I plot my results I obtain the number of sliding movements ("Sliding Steps") in my x-axis, but instead of steps I need years in my x-axis.
In order to change the values of x-axis, I applied the same procedure ("sliding window") but in this case, only for a vector of years and then I calculated the "mode" of each "window" I obtain a 1124 x 1 array that contains the years of each window. How i can change those "steps" by "years" in the array?
You are looking for the axis-properties XTick and XTickLabel of Matlab plots. The documentation by Mathworks provides a useful example at http://www.mathworks.de/de/help/matlab/creating_plots/setting-axis-parameters.html#f6-29060
Instead of the values in the interval of -pi/2 to pi/2 you want to place the years.

Plot time in Matlab [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Plottig data - time plotting
I have problem with ploting time in MATLAB;
Time is in form: HH:MM:SS.FFF, for example (10:56:43.428)
How to plot this? I tried with "datenum(Time, 'HH:MM:SS.FFF'); and plot, and then datetick;
But this is not OK, when I zoom graph there is no more "time" on x-axis.
Is there any other way to plot time directly, so i can use "zoom" and still seeing time x axis?
Second Question: (I would like to start my time from 0 to 20 minutes, so I want my vector 'Time', substract all values with first, so I will get time from 00:00.FFF to end time... How can I do this in Matlab?
Thank a lot for your help, i tried to find help in matlab help but I didn't get any useful helpful.
From the Matlab documentation on datenum
Calling datetick sets the TickMode of the specified axis to 'manual'. This means that after zooming, panning or otherwise changing axis limits, you should call datetick again to update the ticks and labels.
So simply call datetick again after you zoom in