Matt Sal
Matt Sal

Reputation: 41

Plotting x-axis in MatLab

The following variables are used:

I have eight data points. For every 8 data points on x-axis, I have the following labels:

Asset1 - Asset2 - Asset3 - Asset4 - Asset5 - Asset6 - Asset7 - Asset8

Problem: when I implement my code, I get an infinite number of the above labels.

This is my output from MatLab:

my output

This is my code:

SP = rand(8,1)/100;
YP = (rand(8,1)/100)*2;
plot(YP,'DisplayName','YP');
hold on;
plot(SP,'DisplayName','SP');
hold off;
title('SP and YP monthly returns');
xlabel('Monthly time series');
MY = 'Siemens SAP Daimler Allianz DEU.Telekom Adidas BMW DEU.Bank';
set(gca, 'xTickLabels', 'Asset1 Asset2 Asset3 Asset4 Asset5 Asset6 Asset7 Asset8');
xticklabel_rotate('Asset1 Asset2 Asset3 Asset4 Asset5 Asset6 Asset7 Asset8');
ylabel('Percentage of prices discounts');
set(gca, 'yTickLabels', num2str(100.*get(gca,'yTick')','%g%%'));

Upvotes: 1

Views: 145

Answers (1)

gnovice
gnovice

Reputation: 125874

You need to use a cell array of character arrays for 'XTickLabel', not a character array:

labelCell = {'Asset1' 'Asset2' 'Asset3' 'Asset4' 'Asset5' 'Asset6' 'Asset7' 'Asset8'};
set(gca, 'xTickLabel', labelCell);

When you pass a character array like you did, MATLAB just recycles the whole thing for every tick label. You'll have to pass a cell array to xticklabel_rotate as well if you still want to rotate the labels. However, newer versions of MATLAB allow you to do this by modifying the 'XTickLabelRotation' property:

set(gca, 'XTickLabelRotation', 45);  % Rotate by 45 degrees

Upvotes: 3

Related Questions