How can I print multiple texts that change using a for statement?

2 views (last 30 days)
for i = 1:100
J = robot.jacob0(th);
Jp = J(1:3,1:3);
dq = inv(Jp)*[0.01;0;0];
th_new = th;
th_new(1:3) = th_new(1:3) + dq';
robot.plot(th_new)
th = th_new;
getframe(gcf)
x1 = robot.fkine(th_new);
x2=x1.t(1,1);
y1 = robot.fkine(th_new);
y2=y1.t(2,1);
z1 = robot.fkine(th_new);
z2=z1.t(3,1);
text(3,1.5,3,{'X :' num2str(x2)}); % x2 keeps changing, but the previous text is not erased, so it looks overlapped when printed
text(3,1.5,1.5,{'Y :' num2str(y2)}); % The same goes for the text below.
text(3,1.5,0,{'Z :' num2str(z2)})
joint1angle = dq(1,1) * 180/pi;
joint2angle = dq(2,1) * 180/pi;
joint3angle = dq(3,1) * 180/pi;
text(-1,3,3,{'joink 1 angle :' num2str(joint1angle)});
text(-1,3,1.5,{'joink 2 angle :' num2str(joint1angle)});
text(-1,3,0,{'joink 3 angle :' num2str(joint1angle)});
clear
end

Accepted Answer

Voss
Voss on 14 May 2022
Create the text(s) you need once, before the loop, and then update them inside the loop.
% t: 1-by-6 array of text objects
% (Positions don't change, set them now, and
% initialize with empty String)
t = [ ...
text(3,1.5,3,'') ...
text(3,1.5,1.5,'') ...
text(3,1.5,0,'') ...
text(-1,3,3,'') ...
text(-1,3,1.5,'') ...
text(-1,3,0,'') ...
];
for i = 1:100
% ...
% your code to calculate x2, y2, z2, joint1angle, etc.
% ...
% update the Strings of t (adjust the format as necessary, e.g.,
% put them back as cell arrays if you want multi-line text):
set(t(1),'String',sprintf('X : %.2f',x2));
set(t(2),'String',sprintf('Y : %.2f',y2));
set(t(3),'String',sprintf('Z : %.2f',z2));
set(t(4),'String',sprintf('joint 1 angle : %.2f',joint1angle));
set(t(5),'String',sprintf('joint 2 angle : %.2f',joint2angle));
set(t(6),'String',sprintf('joint 3 angle : %.2f',joint3angle));
end

More Answers (0)

Categories

Find more on Characters and Strings 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!