X-label additional text automatically.

Dear all,
I need help about Figures in Matlab. Is it posible to put on x-label two separate texts, for intance one in middle and one od the right edge of figure, but automaticly without command 'text'. For example like in the fig below (I marked it with the red circle):
If I use command xlabel for Frequency then I need to use comand text for (a) and in command tect I need to put cordinates and I do not want to do that for every figure. I need automatical way.

4 Comments

What's keeping you from just adding the call to text() in the script/function that does the rest of the plot?
The point is if I use text() I need to put coordinates always text(x,y). If I have 30 different figures to plot than I need to do 30 different positioning of text, which is lot of work.
I just need command that will do automaticly: one xlabel in center an one xlabel centered right in the same line like in the figure that I upload.
If You have sugestion please write code. Thanks in front!
dpb
dpb on 11 Dec 2020
Edited: dpb on 11 Dec 2020
" I need to do 30 different positioning of text, ..."
Why is it any different? Looks like the axes would be the same or at least very similar.
You have the x values in order to be able to plot the data, code them in, too...text() uses the axes coordinates for location; at worst you have to work out the y coordinate once to align it vertically where desired.
It is different because all 30 pictires are different, some are in time domain, some i frequency etc...All different values of x an y axis.
I can do this for example but I do not want in this way:
plot(t,sig,'LineWidth',1)
xlabel('{\it t} [s]','FontName','times','FontSize',8)
text(0.358,-39.4,'(a)','FontName','times','FontSize',8,'Color','k')
But in this way for every new figure coordinates in comand text are different and I need manual settings of this values each time and I do not want to do that, I want code for automatic positioning like in the second line of example xlabel.
If You know how to do that automaticly can write it down.

Sign in to comment.

 Accepted Answer

Try something like this:
x = 1:10;
y = randn(1,10);
figure
plot(x,y)
xlabel('Frequency [Hz]')
text(max(xlim), min(ylim)-diff(ylim)*0.1, '(a)', 'HorizontalAlignment','right', 'VerticalAlignment','middle')
It seems to work correctly in my simulations, and appears to produce constant relative positioning of ‘(a)’.

6 Comments

It did not provide me good position for y value but it is ok for x, so we are close :)
Is it posible to know automaticly y-value for xlabel?
What is the problem with the y-value? It always gives appropriate results in my simulations, even when I change ‘y’ to:
y = randn(1,10)*10 + 10;
that appears to approximate the y-axis range in the image you posted.
What are typical values in your data for ylim?
Note that I set the constant multiplier at 0.1 in my example code. This positions the text results with respect to the y-axis. You may need to vary that for my text call to work correctly with your data, depending on where you want the ‘(a)’ to appear, with smaller values moving it up (closer to the x-axis), and larger values moving it down (farther from the x-axis). I assumed that you want it at the same y-axis offset as the xlabel string, and my code puts it there. (I am using R2020b, Update 3.)
Ok, example of code:
clear all;
close all;
N = 1000;
t = 0:0.001:1;
x = 1.5*sin(4*pi*t);
%x = x - sign(t-0.3) - sign(0.72-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])
%grid on
xlabel('{\it t} [s]','FontName','times','FontSize',8)
ylabel('{\it A} [rel. units]','FontName','times','FontSize',8)
text(max(xlim), min(ylim)-diff(ylim)*0.1, '(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])
I bold a part You send me. It gives me (a) on good position on right but not in line with xlabel. Picture:
You need to change the multiplier:
text(max(xlim), min(ylim)-diff(ylim)*0.20, '(a)', 'HorizontalAlignment','right', 'VerticalAlignment','middle','FontName','times','FontSize',8)
↑ ← THIS VALUE
Changing it to 0.2 appears to position it correctly in your code.
Changing the code slightly:
posmult = 0.2; % Position Multiplier
text(max(xlim), min(ylim)-diff(ylim)*posmult, '(a)', 'HorizontalAlignment','right', 'VerticalAlignment','middle','FontName','times','FontSize',8)
This may be a more straightforward way of specifying the value. Increase the value of ‘posmult’ to move the text ‘(a)’ down (away from the x-axis) and decrease it to move it up (toward the x-axis).
Thanks a lot. It have manuel setting but not as my firts code so it is ok, I only need to change posmult like You said!
Thank You again!
As always, my pleasure!

Sign in to comment.

More Answers (1)

Categories

Community Treasure Hunt

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

Start Hunting!