0

I am having a problem with Matlab at the moment and I hope you can help me. I have written this code below to display a graph, but now I want to add another x-axis above the image, which I can scale myself. Is there any possibility to add another line like: set(gca, 'XTick2', [bla:bla:bla]); and another label?

EDIT: I have solved a part of the problem, hope you can help me with the rest, too... I have now 2x-axes, but still a few problems.
The labels are in wrong positions, the y-label is inside the scale and I want two different labels for the two x-axes.
Also I would like to remove the scale of the lower x-axis from the upper one.
Code is also the new one:

x1 = [0, 421.441, 842.882, 1264.323, 1685.764, 2107.205, 2528.646, 2950.087, 3371.528, 3792.969, 4214.41, 4635.851, 5057.29];
y1 = [55.659, 55.856, 56.081, 56.279, 56.312, 56.169, 56.038, 55.903, 55.75, 55.604, 55.512, 55.534, 55.661];
y2 = [51.231, 51.735, 52.063, 52.152, 51.632, 51.16, 51.014, 50.911, 50.721, 50.596, 50.597, 50.858, 51.242];
y3 = [50.939, 51.381, 51.644, 51.687, 51.353, 50.944, 50.829, 50.706, 50.538, 50.43, 50.412, 50.614, 50.948];
y4 = [50.023, 50.328, 50.506, 50.535, 50.352, 50.113, 50.032, 49.938, 49.801, 49.705, 49.672, 49.801, 50.03];
plot(x1,y1, 'ks-',x1,y2, 'bx--',x1,y3, 'gd-.',x1,y4, 'c.-'),
ax1 = gca;
ax1.XLim = [0, 5075];
ax1.XTick = 0:1000:5075;
ay1 = gca;
ay1.YLim = [49.5, 56.5];
ay1.YTick = 49.5:0.5:56.5;
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.XLim = [0, 360];
ax2.XTick = 0:30:360;
ax2.XAxisLocation = 'top';
ax2.YTick = [];
grid off
xlabel('Time [s]')
ylabel('Temperature [°C]')
legend('A','B','C','D')
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Zen_Master
  • 21
  • 7
  • Maybe have a look at [this](http://stackoverflow.com/questions/31009357/how-to-insert-two-x-axis-in-a-matlab-a-plot/31009945#31009945) question it looks like what you want to achieve. – Benoit_11 Jun 24 '15 at 12:20
  • the answer helped me a bit, but i still have problems with the labels. Thank you. The new problem is edited above. – Zen_Master Jun 24 '15 at 13:18

1 Answers1

1

Just use the function text,

text(x,y,'string') 

where x and y are the coordinates of you new x-axis and string your blabla.

UPDATE: use this sample code and adjust it to your needs

x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;

figure
line(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
    'XAxisLocation','top',...
    'YAxisLocation','right',...
    'Color','none');

which gives the plot,

enter image description here

SamuelNLP
  • 4,038
  • 9
  • 59
  • 102
  • I need it to have an own scale, which is going from 0° to 360° in 30° steps... just adding text doesn't really help. But thank you for your quick reply. – Zen_Master Jun 24 '15 at 11:42
  • what do you mean by own scale? can you present a sample image? – SamuelNLP Jun 24 '15 at 13:30
  • i cant,my reputation is too low, but i uploaded it to imigur... http://imgur.com/5xPyFh0. As i mentioned above, the proplem is partially solved... the labels are a problem and the scale of the lower x-axis is shown in the upper x-axis ,too – Zen_Master Jun 24 '15 at 18:07