Why is my annotation textbox not displaying text?

I'm working on a function to allow a GUI user to type an annotation and then click and drag on a figure to create the location of a textbox annotation. However, when I change the options of the annotation, the text no longer appears. The following is my exact code.
function annotate_plot(input_text)
set(gcf,'pointer','crosshair')
set(gcf,'WindowButtonDownFcn', @clickFcn, 'WindowButtonUpFcn', @unclickFcn);
function clickFcn(varargin)
if ~strcmpi(get(gco, 'type'),'figure')
global pt1
pt1 = get(gca,'CurrentPoint');
end
end
function unclickFcn(varargin)
if ~strcmpi(get(gco, 'type'),'figure')
global pt2
pt2 = get(gca,'currentPoint');
place_annotation()
else return
end
end
function place_annotation(varargin)
global pt1 pt2
box = [pt1(1,1), pt1(1,2), pt2(1,1)-pt1(1,1), pt2(1,2)-pt1(1,2)];
ah = annotation('textbox','string',input_text);
set(ah,'parent',gca);
set(ah,'position',box);
end
end
This is what happens on running it:
%

Answers (1)

I have the same problem. What I noticed was that acutally there is still text. It is just far outside of the box. It happens as soon as I do
set(ah,'parent',gca);
I get a tiny box and text when I write
>> plot(linspace(0,10,10),linspace(0,10,10));
>> ah=annotation('textbox');
>> set(ah,'parent',gca);
>> set(ah,'string','test');
Setting some usefull position as a next step seems to move the text outside of the figure, as shown in the previous post.
I need to place a textbox at an exact value in the plot (not via normalized values). Are there alternative methods?

2 Comments

Are you trying to do it with a click-and-drag type interface? Or just an input value for x and y and a text string?
I don't try to creat any user interface. I just wanted to pin a string to a specific data point. A few minutes ago I found the text() function, which does exactly what I want. I struggeld way to long for this one ...
Still, I guess my post may be not completely useless as it describes the problem in a little (tiny) bit more detail.

Sign in to comment.

Asked:

on 29 Jul 2015

Commented:

on 1 Dec 2017

Community Treasure Hunt

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

Start Hunting!