Annotation Displays in the Wrong Location
Ältere Kommentare anzeigen
Using Matlab 2020b, I am trying to dislplay an annotation with code like the block shown below. The functions x_to_norm_v2 and y_to_norm_v2 are from the Mathworks website, here:
The first figure below is how it looks when I run the code. The second figure is how I want it to display. I found that if I set a breakpoint right after the first line of code below (the line that defines fg1 as a figure), it displays properly. How can I fix this so it displays properly without having to set a breakpoint?
fg1 = figure('NumberTitle', 'off', 'WindowStyle', 'docked');
numPltRws = 2;
numPltCls = 2;
pltndx1 = 0;
chrt = tiledlayout(numPltRws, numPltCls);
pltndx1 = pltndx1 + 1;
plt1(pltndx1) = nexttile;
loglog(1, 1, 'ok');
[x_norm] = x_to_norm_v2(3,1);
[y_norm] = y_to_norm_v2(1,1);
annotation('textarrow', [x_norm], [y_norm], 'String', 'Annotation');
grid
The output looks like:

5 Kommentare
DGM
am 17 Apr. 2023
I can't reproduce the error, but it might have something to do with figure properties not being completely up to date by the time they start being used. Does adding a pause(1) after figure creation change anything?
DH
am 17 Apr. 2023
DGM
am 17 Apr. 2023
I don't know if a drawnow() would work for this case, but if it does, it might be more robust than an arbitrary time delay.
DH
am 17 Apr. 2023
DH
am 20 Apr. 2023
Antworten (1)
Walter Roberson
am 17 Apr. 2023
1 Stimme
What is happening is that by default the coordinates used for annotation() are normalized to the figure.
The call to figure() will always involve a flush of the graphics queue, but you then create tiled layouts and loglog(), and the positions of those graphics objects will not be finalized until the next time the graphics queue is flushed.
Unfortunately, annotation does not provide any way to specify data coordinates; https://www.mathworks.com/help/matlab/ref/matlab.graphics.shape.textarrow-properties.html#buchvf6-1_sep_shared-Units or even to use normalized units relative to the axes. The closest you could get would be to created a uipanel around each axes, as you can normalize to a container such as uipanel.
Or you can look in the File Exchange, as there are some contributions there that can assist in providing annotations in data coordinates. The better of the contributions hook into the figure or axes size change callbacks and re-calculate the coordinates as needed.
Kategorien
Mehr zu Annotations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!