MATLAB Answers

Xlabel coordinates for text command?

9 views (last 30 days)
Djordje Damnjanovic
Djordje Damnjanovic on 13 Dec 2020
Hello everybody!
I have question. I'm using matlab 2014b and I need to find coordinates of xlabel text on the figure. That coordinates I will use later in the text() command for something else. So it is like this:
  • I have this part in Figure:
xlabel('{\it t} [s]','FontName','times','FontSize',8)
And then I do this:
x1=get(get(gca,'XLabel'),'Position');
x1 returns as a vector with 4 coordinates x1[a b c d] where a is x-coordinate, b is y-coordinate, c is weight and d is height if I'm right.
Now if I use a and b values from x1 vector for text() command:
text(a , b, '(a)', 'HorizontalAlignment','right', 'VerticalAlignment','middle','FontName','times','FontSize',8);
(a) is not in the same spot as xlabel was.
So my question is where do I make mistake? is the units different in x1 and text?
Basicly I need do obtain exact same coordinates for xlabel, put it in text command for some other text in figure.

Answers (3)

Mario Malic
Mario Malic on 13 Dec 2020
Edited: Mario Malic on 13 Dec 2020
Hi,
XLabel and Text are of the same type, so they have same properties. Default settings for text are center and middle alignments, but you align your text on the right side which probably is the issue.
xLabel = get(gca,'XLabel');
labelText= text(a , b, '(a)', 'HorizontalAlignment','right', 'VerticalAlignment','middle','FontName','times','FontSize',8);
The bottom command saves the handle to the text label, you can open it in property browser and inspect if there are some unusual differences between label and text.
Note: You have version 2014 and nowadays it's different, there are only 3 coordinates that define Position property of text. Consult with documentation and check what exactly these are, if you have 4 coordinates.
  1 Comment
Djordje Damnjanovic
Djordje Damnjanovic on 14 Dec 2020
Command xLabel = get(gca,'XLabel'); returns one value in Matlab 2014b.

Sign in to comment.


Matt Gaidica
Matt Gaidica on 13 Dec 2020
h = figure('position',[0 0 600 800]);
x = xlabel('{\it t} [s]','FontName','times','FontSize',12);
text(x.Position(1) , x.Position(2), '{\it t} [s]', 'FontName','times','FontSize',12,...
'HorizontalAlignment',x.HorizontalAlignment, 'VerticalAlignment',x.VerticalAlignment);
It will become unaligned if you manually resize vertically. If this doesn't work, maybe explain your application a little more? It seems like a hack.
  1 Comment
Djordje Damnjanovic
Djordje Damnjanovic on 14 Dec 2020
Hello! Thanks for help. In Your code x returns one value when I run it, so it doesn't work. Ok I will upload whole code to see what I want to do.

Sign in to comment.


Djordje Damnjanovic
Djordje Damnjanovic on 14 Dec 2020
Ok thanks again for sugestion! Here is a thing. I post it before that I need to find and place some thing in the figures automaticly and one of you from comunity help me 50% which is ok. But then I figure it out that maybe it can be done another way.
Ok lets start with the code. This is simple code, I will use it for more complex figures etc.
clear all;
close all;
N = 1000;
t = 0:0.001:1;
x = 1.5*sin(4*pi*t);
sig = x + 0.2*randn(size(t));
lev = 5;
wname = 'db2';
dnsig1 = wden(sig,'minimaxi','h','mln',lev,wname);
figure(1)
set(gcf, 'Units', 'centimeters');
set(gcf,'Position',[20 2 16 4])
subplot(131)
plot(t,x,'LineWidth',1)
ylim([-2 2])
xlim([0 1])
xlabel('{\it t} [s]','FontName','times','FontSize',8)
ylabel('{\it A} [rel. units]','FontName','times','FontSize',8)
x1=get(get(gca,'XLabel'),'Position');
text(max(xlim), x1(2), '(a)', 'HorizontalAlignment','right', 'VerticalAlignment','middle','FontName','times','FontSize',8)
set(gca,'xtick',[0 0.2 0.4 0.6 0.8 1],'FontName','times','FontSize',8)
set(gca,'ytick',[-2 -1 0 1 2],'FontName','times','FontSize',8)
set(gca, 'Units', 'centimeters');
set(gca,'Position',[0.9 0.9 4 2.9])
Ok here is a thing. I want to put xlabel and (a) or (b) or (c) in line with xlabel. Xlabel will be centered and (a) will be right oriented like in figure:
So if I do the code that I put it in this post I get this figure:
So as you can see in the second figure (a) is not located in line with xlabel: t[s]. In the first figure I did manualy. Any sugestions?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!