How to set xticklables to certain places in the graph in matlab - 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);

Related

Changing tick values of matlab .fig

I have a matlab figure given to me where the x axis values range from 0 to 4500 time steps. Each time step corresponds to 1.8e-8 seconds. I want to convert the ticks to seconds so I have 0,0.0900,0.1800,0.2700,0.3600,0.4500,0.5400,0.6300,0.7200,0.8100 seconds.
Is there a way to do this in the figure plotter?
If you don't want to modify the actual data, then once you have opened the .fig file you can change the labels simply by doing
set(gca, 'XTickLabel' , {'0' '0.09' '0.18' '0.27' '0.36' '0.45' '0.54' '0.63' '0.72' '0.81'})
If instead you want to modify the data, then after opening the figure file you can first get the handle to the axis with gca, then next get the handle of the plot, and finally get the underlying data and modify it:
ax1 = gca; % get the handle to the axis
plt1 = get(ax1,'Children'); % get the handle to the plot
xdata = get(plt1,'XData'); % retrieve x data from the plot
xdata_seconds = xdata * 1.8e-4; % convert x data to desired units
set(plt1, 'XData', xdata_seconds) % put the new data into the plot
You can simply look for the children plot in the axis and modify its XData:
x = 1:4500;
y = rand(size(x));
ax = axes;
plot(ax, x, y, '-r');
The plot now will have ticks from 1 to 4500.
ax.Children.XData = ax.Children.XData.*1.8e-8;
and now it is instead converted in seconds.
If the axes contain more than one children, you might want to find the correct one and use its index in the call ax.Children(idx).XData.

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;

MATLAB: how to customize non linear X axis (for example ticks at 1,2,3,4,5,20,100,'string')

I'm using the MATLAB plot feature to compare two vectors. I would like my X axis to represent 1 through 7, and then 14, 21, and then a category at the end for points with undetermined X values..(I'm also not sure how to represent these numberless point (they have Y values, just no X values) I could assign a large number outside any of my X values (1000) to these points, do the 1-7,14,21,1000 and then change the 1000 label to my 'string for un-numbered points'. ??
Here is a way to do it based on the example by The Mathworks here.
The trick is to create 2 x axis with different ranges and make one of them transparent. You can then play around with its properties. I modified a bit their code but kept most of their comments because they explain well the steps.
For the demo I used scatter to represent the points and colored the "good" points in red and the other point (here only 1) in green. You can customize all this of course. For instance I used a value of 100 and not 1000 for the x value of the 2nd axes, but I'll let you figure out how to modify it all to change the output as you wish.
clear
clc
close all
%// Create axes 1 and get its position
hAxes1 = axes;
axes_position = get(hAxes1, 'Position');
%// Create axes 2 and place it at the same position than axes 1
hAxes2 = axes('Position', axes_position);
%// Your data go here.
x = [1 7 14 21 100];
y = rand(1, length(x));
%// Plot the two sections of data on different axes objects
hPlot1 = scatter(hAxes1, x(1:4), y(1:4),40,'r','filled');
hold on
hPlot2 = scatter(hAxes2, x(end), y(end),40,'g','filled');
%// Link the y axis limits and fontsize property of the axes objects
linkaxes([hAxes1 hAxes2], 'y');
linkprop([hAxes1 hAxes2], 'FontSize');
%// Set the x range limits and tick mark positions of the first axes object
set(hAxes1, 'XLim', [1 25], ...
'XTick', [1 7 14 21])
%// Set the x range limits and tick mark positions for the second axes object.
%// Also set the background color to 'none', which makes the background
%// transparent.Add the label for the "super points".
set(hAxes2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [95 100], ...
'XTick', 100,'XTickLabel',{'Super Points'})
And the output:
EDIT
If you wish to add a fit line, I suggest adding a 3rd axes at the same position than the other 2, make it transparent and remove its X- and Y-ticks.
i.e. Add something like this:
hAxes3 = axes('Position', axes_position,'Color','none','YTick',[],'XTick',[]);
And in the call to polyfit/polyval, in my example you want to get only the first 4 elements (i.e. the red ones).
Hence:
p = polyfit(x(1:4),y(1:4),1);
y_poly = polyval(p,x(1:4));
And then plot the line on hAxes3:
hPlot3 = plot(x(1:4),y_poly)
Output:

Bar graph starting from zero

I have a vector with 11 elements. I want to display the graph as in this picture:
but my graph starts from first month. How can I make it start from the zeroth month?
You can use both an x and y input argument for bar.
ax = axes();
x = 0:11;
bar(x,y);
If that doesn't give you the plot you want you can also use the Xlim, XTick, and XTickLabel properties of to control how the x axis looks.
set(ax, 'Xlim', [0,11])
set(ax, 'XTick', [0:11])

Creating Surf() with Labels

[ndata, text, alldata] = xlsread('Euro swap rates.xlsx',3);
%(although created from text dates is still a cell array?)
dates=text(:,1);
%(Same problem here)
rates_header=text(1,:);
rates=ndata;
surf(rates);
colormap(jet);
>> surf(rates,dates); %but when I try to add wither of the labels I get a problem?
??? Error using ==> surf at 78
X, Y, Z, and C cannot be complex.
>> surf(rates,dates,rates_header);
??? Error using ==> surf at 78
Z must be a matrix, not a scalar or vector.`
I'm assuming the problem is because dates and rates_header are still cell arrays?
How can I convert them to text? Is there a way to do it directly as part of xlsread?
Lastly on the plot I would like to make the first cell in the arrays dates and rates_header, the name for that axis of the surf plot with all the rest of the data being used to populate the axis.
getting closer
title('Euro Swap Rates');
xlabel('Maturity');
ylabel('Date');
zlabel('Swap Rate');
set(gca, 'YTick', 1:100:length(dates));
set(gca, 'YTickLabel', dates(1:100:length(dates)));
set(gca, 'XTick', 0:10:length(rates_header));
set(gca, 'XTickLabel', rates_header(0:10:length(rates_header)));
Two questions remain:
I would like the x Tick to be 1y then 10y20y...60y So that the first step size is 9y then 10y for all remaining points
I would like the dates to show the 1st of Jan and ist of June each year only (or the closest working days to those dates).
It looks like you want to set the labels on the x-axis.
You don't do this through surf, you do it after using set, like this:
set(axHandle, 'XTickLabel', xlabels);
Here is a complete example:
[x y] = meshgrid(-4:.25:4;);
z = x.^2 + y.^2;
xlab = {'one','two','three','four'};
surf(x,y,z);
set(gca,'XTickLabel', xlab);
You can use whatever labels you want provided that they are strings and saved to a cell array. This is what I did above for xlab.