How do I display custom text for the data cursor?

31 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2016 年 12 月 2 日
編集済み: MathWorks Support Team 2021 年 4 月 19 日
I want to customize the text that is displayed in the text box when I use the data cursor tool.
I want to use my own labels instead of the standard X and Y coordinates that are being displayed. How do I do this? 

採用された回答

MathWorks Support Team
MathWorks Support Team 2016 年 12 月 2 日
For doing this, you need to make use of the "Edit Text Update Function" from the Data Cursor Tool context menu. 
Here is a detailed example that walks you through the different steps involved. 
1. Create a simple scatter plot as follows : 
>> scatter(rand(1,10), rand(1,10));
2. After the plot opens, click on the "Data Cursor" tool. 
3. Right click anywhere inside the plot, and select "Edit Text Update Function".
4. Edit the code in such a way that the "output_txt" variable is the label you want to display next to the selected point. This example function displays "High Risk" if both "x" and "y" values are greater than 0.5, and "Low Risk" otherwise :
====
function output_txt = display_risk(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
% Get the position of the point being clicked on
pos = get(event_obj,'Position');
% If both X and Y values are greater than 0.5, label it as high risk
% Else label it as low risk
if pos(1) > 0.5 && pos(2) > 0.5
output_txt = {'High Risk'};
else
output_txt = {'Low Risk'};
end
====
5. Save the file with the appropriate name. In this case "display_risk.m"
6. The "Data Cursor" should now display the custom labels according to the point being selected.
7. To use the same functionality for another plot, select the "Data Cursor" tool, right-click and select "Select Text Update Function", and choose the appropriate function you want to use. 

その他の回答 (1 件)

Giuseppe Naselli
Giuseppe Naselli 2018 年 3 月 28 日
編集済み: Giuseppe Naselli 2018 年 3 月 28 日
Hi, thanks for this but the explanation is a bit vague.
  • Could you please explain a bit better where the display_risk.m needs to be saved?
  • And if we want to include it in a stand-alone function that is shared with others?
  • is there a way of making the display_risk function to read data from the workspace?
  • In other words, suppose I want to create a function that has x,y,z as inputs where x = coordinate x,y = coordinate y,z = text to be displayed when the cursor clicks on the corresponding point
Thanks in advance G
  1 件のコメント
Steven Lord
Steven Lord 2018 年 3 月 28 日
編集済み: MathWorks Support Team 2021 年 4 月 19 日
The third example on the documentation page for the datacursormode function shows how to set a custom data cursor update function programmatically. Since you say you want to "include it in a stand-alone function that is shared with others" that may be easier for your application.

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

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by