How can I automatically specify a "Best Location" property for the textbox annotation function?

조회 수: 29 (최근 30일)
Can I place a textbox annotation at the 'best' location of a plot instead of needing to manually specify the coordinates? I would like to do something like the following:
>> t = annotation('textbox', 'String', 'Hello', 'Location', 'best');
This results in an error because there is no Location property on the TextBox class.
The 'legend' function can use 'best' to intelligently decide where to place the graphic element on a plot. Additionally, it can specify the 'Location' to be in the cardinal directions of the plot such as 'east', 'southwest', etc. It would be great if there were similar functionality in a function which places only text on the plot.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2017년 5월 9일
To automatically place a text object at the 'Best' location similar to a 'legend' object, use the attached MATLAB function 'TextLocation'. To use this, execute the following steps:
1. Download the attached function and store it in a location that is in MATLAB path.
2. Call this function with required text after creating the figure. For example:
a. Insert the text 'Hello' in the 'best' location in the current figure:
plot(1:10);
TextLocation('Hello','Location','best');
b. Insert the text 'World' in the 'southwest' location in the current figure:
TextLocation('World','Location','southwest');
Please note that the use of this function will delete any legend currently in the figure window. If you wish to use a legend in your figure, please specify it after calling the 'TextLocation' function.
  댓글 수: 1
DGM
DGM 2022년 2월 9일
I should point out that this will not only delete the legend for the current axes, but it will also overwrite the DisplayName property of the first plot object in the axes. So do this before you configure either.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by