Need help with a simple task of putting a textbox and a circle marker on a surface point?

1 view (last 30 days)
Hi guys,
I have this simple code:
x = [0:10]; y = [0:10];
f1 = 3.45645; f2 = 5.4542121;
Formula1 = @(x,y)(x+y);
[X1,Y1] = meshgrid(x,y);
Z1 = Formula1(X1,Y1); Z2 = Formula1(f1,f2);
surf(X1,Y1,Z1);
hold on
plot3(f1,f2,Z2, 'ko');
hold off
str = {strcat('X: ',num2str(round(f1,2))), strcat('Y: ',num2str(round(f2,2))), strcat('Z: ',num2str(round(Z2,2)))};
annotation('textbox', [0.2,0.4,0.1,0.1],'String', str);
I want to have a round CIRCLE at [f1,f2,Z2] and also have a textbox right around that which displays the values of that circle: "X: 3.45; Y = 5.45; Z = ...".
But I am having difficulty placing the textbox close to where the circle is because I think it uses different units? Also, I can't seem to add enough spacing after the X: or the Y:...
Please help. Thanks!

Answers (1)

Jan
Jan on 1 Jun 2015
strcat removes marginal spaces in the strings. So either use horzcat or the equivalent [ and ], or include the strings in curly braces to obtain cell strings:
str = {strcat({'X: '}, num2str(round(f1,2))), ...
strcat({'Y: '}, num2str(round(f2,2))), ...
strcat({'Z: '}, num2str(round(Z2,2)))};
or
str = {['X: ', num2str(round(f1,2)))], ...
['Y: ', num2str(round(f2,2)))], ...
['Z: ', num2str(round(Z2,2)))}};
I'd prefer:
str = {sprintf('X: %.2f', f1), ...
sprintf('Y: %.2f', f2), ...
sprintf('Z: %.2f', Z2)};
The annotation uses normalized coordinates relative to the figure. |text is simpler to insert text at children of an axes object:
text(f1, f2, Z2, str, 'Margin', 5);

Community Treasure Hunt

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

Start Hunting!