How do I display custom text for the data cursor?
26 次查看(过去 30 天)
显示 更早的评论
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
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.
0 个评论
更多回答(1 个)
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
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 Exchange 中查找有关 Language Support 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!