Function like text() but with auto placement like legend?

Does such a function exist which simply addes some text to a plot, without having specify the x/y location, and without also adding a line/marker graphic?
In other words, I am looking for a function which has output like text() (i.e., no other graphics, like legend() has), but which has the ability to intelligently place the text object, as in legend(), i.e.
legend(____,'Location',_____)

7 件のコメント

Stephen23
Stephen23 2017 年 5 月 3 日
"Does such a function exist which simply addes some text to a plot, without also adding a line/marker graphic?"
text()
Evan
Evan 2017 年 5 月 3 日
text() requires a location specification.
Jan
Jan 2017 年 5 月 3 日
Of course text() requires a position. Where shoudl it be placed without a location? In the upper left or in the center or outside the diagram? Matlab cannot guess this detail.
"Without adding a line/marker graphic"? text() does not do this, so why do you ask for this detail?
Please explain what you want to achieve. Omitting an required detail is not enough to specify, which behavior you want to get.
Evan
Evan 2017 年 5 月 3 日
Hi Jan, thanks for your response. legend() does not require a [numeric] location specification. I've updated the question to clarify. I thought between the title and the body, the aim was clear.
Jan
Jan 2017 年 5 月 3 日
"Intelligently" sounds fine, but what is this? Where is an "intelligent" location for a text? I cannot imagine a situation where I want text to appear at a location I do not explicitely define.
Do you want to create a text at a location, which is not covered by a graphics object? Ah, this would be another story.
dpb
dpb 2017 年 5 月 3 日
OTOH, as noted in the Answer below, I can think of a multitude of instances wherein I would like text to be located "without being touched by human hands". :)
That's certainly one "intelligent", locating paradigm, yes...as noted, the one example I can think of presently in Matlab is legend with the 'best' location property value. It isn't always perfect, but is, on the whole in my experience, better than a random choice of a fixed location when one isn't positive a priori what the shape of the plotted data is.
Evan
Evan 2017 年 5 月 3 日
編集済み: Evan 2017 年 5 月 4 日
@Jan, Are you saying you don't think the "location','best' functionality of legend() is useful?
"Do you want to create a text at a location, which is not covered by a graphics object? Ah, this would be another story."
Yes

サインインしてコメントする。

 採用された回答

dpb
dpb 2017 年 5 月 3 日
編集済み: dpb 2017 年 5 月 3 日

1 投票

No, altho I can guess the desire...would be kind of a nice touch if Matlab could/would automagically locate a desirable place to place a label for a line label, etc., without having to compute it. It's sometimes difficult to know programmatically where to place it so doesn't obstruct the already existing information on the axes.
Only thing I can think of at the moment is there are the 'best'|'bestoutside' values for the 'Location' property in legend but I don't think there are any other user functions with similar abilities. Some of the CurveFitting tools may label stuff, I'm not sure??? but those are applications, not user functions.
What the hey, submit an enhancement request...
PS. I didn't search but there's always a possibility somebody else has had the similar yen and has written/uploaded a solution on File Exchange.

2 件のコメント

Evan
Evan 2017 年 5 月 3 日
編集済み: Evan 2017 年 5 月 4 日
Enhancement request it is! Specifically, the request is for the location-based options used in legend() to be added to the "textbox" annotation.
https://www.mathworks.com/help/matlab/ref/textbox-properties.html
Katelyn
Katelyn 2022 年 7 月 2 日
legend

サインインしてコメントする。

その他の回答 (4 件)

Walter Roberson
Walter Roberson 2017 年 5 月 3 日

3 投票

In the case where you need only a single such item per axes:
h = line(nan, nan, 'Color', 'none');
legend(h, 'Your Message Here', 'Location', ....)

4 件のコメント

Evan
Evan 2017 年 5 月 3 日
Nice hack. Thanks for the tip.
dpb
dpb 2017 年 5 月 4 日
Yeah, the NaN data in plotting often solves "issues"; didn't think of it in this particular context as was fixated on the idea of labeling lines in the area of the data but missing it...
Pedro Busc
Pedro Busc 2020 年 5 月 26 日
How to remove the space used to the plotted item symbol (in this case none, blank) on the left?
Walter Roberson
Walter Roberson 2020 年 5 月 26 日
You would have to use two or more outputs for legend(), and access the graphic objects such as the text() object and adjust its Position property.
Note that two or more outputs for legend() is now undocumented and unsupported, and could be removed Any Release Now.

サインインしてコメントする。

Evan
Evan 2017 年 5 月 4 日

0 投票

Here's another hack, based on a suggestion from Mathworks support.
function hOut = AutoText(textString,varargin)
l = legend(textString,varargin{:});
t = annotation('textbox');
t.String = textString;
t.Position = l.Position;
delete(l);
t.LineStyle = 'None';
if nargout
hOut = t;
end
Walter Roberson
Walter Roberson 2017 年 5 月 9 日

0 投票

2 件のコメント

Evan
Evan 2017 年 5 月 9 日
編集済み: Evan 2017 年 5 月 9 日
The accepted answer to that question is clearly copied from my answer to this question. The body of the attached function is identical.
That question is odd, too. It was asked 7 years ago, apparently by Mathworks support. You'd think they would have just added the functionality to the Textbox annotation by now...
dpb
dpb 2017 年 5 月 9 日
I suppose the somebody who wrote this one thought it better than whatever was original...

サインインしてコメントする。

IT Department
IT Department 2020 年 1 月 3 日
編集済み: KSSV 2025 年 3 月 11 日

0 投票

There is a function in the Matlab File Exchange that addresses this issue.

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

タグ

質問済み:

2017 年 5 月 3 日

編集済み:

2025 年 3 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by