Add a textbox in a subplots inside a loop
9 views (last 30 days)
Show older comments
I want to create a text box for each subplot that are created inside a loop for. But all the text is created in the fist plot and i don't know how to make it for the second one, also i cannot change the titles of any of the plots.
this is my code so far:
clc
clear
z = [0.44,4.7, 7, 8, 17.9, 31, 57];
dim = [.2 .5 .3 .3];
c ={'0.44','4.7', '7', '8','17.9', '31', '57'};
txt = texlabel('H(s) = (5/z*(s+z))/(s^2+37s+232)');
txt2 = texlabel('H(s) = (5*(s+z))/(s^2+37s+232)');
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
for i=1:length(z)
zc=z(i);
%graph1
num=(5)*[1 zc]; % [] valor de los coeficientes del numerador de H(S)
dem =[1 37 232];
h=tf(num, dem);
step(ax1, h)
hold(ax1, 'on')
text(0.8,1,txt,'FontSize',10,'fontname','times')
grid on
%graph2
num2=(5)/(zc)*[1 zc]; % [] valor de los coeficientes del numerador de H(S)
h2=tf(num2, dem);
step(ax2, h2)
hold(ax2, 'on');
annotation('textbox',dim,'String',txt2,'FontSize',10,'fontname','times','FitBoxToText','on');
grid on
end
legend(ax1, c);
legend(ax2, c);
and thats what i'm getting out of it:
0 Comments
Answers (1)
Jan
on 23 Oct 2021
Move the repeated code out of the loop to simplify the code:
z = [0.44, 4.7, 7, 8, 17.9, 31, 57];
dim = [.2 .5 .3 .3];
c = sprintfc('%g', z); % UNDOCUMENTED
txt = texlabel('H(s) = (5/z*(s+z))/(s^2+37s+232)');
txt2 = texlabel('H(s) = (5*(s+z))/(s^2+37s+232)');
ax1 = subplot(2,1,1, ...
'NextPlot', 'add', ... % Same as: hold on
'Grid', 'on');
text(ax1, 0.8, 1, txt, 'FontSize', 10, 'fontname', 'times');
ax2 = subplot(2,1,2 ...
'NextPlot', 'add', ... % Same as: hold on
'Grid', 'on');
annotation('textbox', dim, 'String', txt2, 'FontSize', 10, ...
'fontname', 'times', 'FitBoxToText', 'on');
dem = [1 37 232];
for i = 1:length(z)
zc = z(i);
%graph1
num = 5 * [1 zc]; % [] valor de los coeficientes del numerador de H(S)
h = tf(num, dem);
step(ax1, h)
%graph2
num2 = 5 / zc * [1 zc]; % [] valor de los coeficientes del numerador de H(S)
h2 = tf(num2, dem);
step(ax2, h2)
end
legend(ax1, c);
legend(ax2, c);
Which text should appear where?
Note that text() creates a text in an axes object (created by subplot() here), but annotation writes to a figure.
3 Comments
Jan
on 25 Oct 2021
What does "still doesn't work" mean? The first message is printed at [0.8, 1] in coordinates of the axes. The 2nd message is shown at [0.2, 0.5] in coordinates of the figure.
See Also
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!