How can I set the text font style of a Data Cursor object?

16 次查看(过去 30 天)
I have created a data cursor object on a plot. Is it possible to customize the font style, size, and/or weight for the text in the data cursor object?

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2019-3-26
You can customize the font style, size, or weight of the text in data cursors by using the following commands:
alldatacursors = findall(gcf,'type','hggroup')
set(alldatacursors,'FontSize',12)
set(alldatacursors,'FontName','Times')
set(alldatacursors, 'FontWeight', 'bold')
The options for 'FontWeight' are 'bold' or 'normal'. Please note that the text may appear to be the same weight for both 'bold' and 'normal' depending on the font chosen, as not all fonts have a bold weight.
You can find a list of available fonts by using the command;
listfonts
The ability to set specific default fonts for data cursors is not currently available. However, one can set the default styles and sizes for all plots. This can be done as follows:
set(0,'DefaultTextFontName','Script') % Sets Font Style
set(0,'DefaultTextFontSize',10) % Sets font size
This must be done before any figures are created or launched. A recommended place for this command is in the "startup.m" file.
  2 个评论
Walter Roberson
Walter Roberson 2019-3-26
hggroup are pretty general objects, and it is not clear that all of them will have font related properties. Even if we exclude the difficulty that not all hggroup with font properties will have to do with data cursors, we should narrow the search down, such as
alldatacursors = findall(gcf, 'type', 'hggroup', '-property', 'FontSize');
DGM
DGM 2025-8-19
编辑:DGM 2025-8-19
As far as I know, PointDataTip properties aren't documented
You can set some text properties from the parent DataTip object, but not 'FontWeight'.
% get all DataTip objects
alldatacursors = findall(gcf,'type','datatip');
% now you can set the properties of the datatips
set(alldatacursors,'FontSize',18)
set(alldatacursors,'FontName','Times')
If you want to set other properties via the child PointDataTip object, you can narrow down the search to avoid picking up other hggroup objects:
% get all PointDataTip objects
% this gets all hggroups -- but an hggroup can be any number of things
alldatacursors = findall(gcf,'type','hggroup');
% so just keep the hggroups which are also datatips
f = @(x) isa(x,'matlab.graphics.shape.internal.PointDataTip');
alldatacursors = alldatacursors(arrayfun(f,alldatacursors));
% now you can set potentially undocumented properties of the datatips
set(alldatacursors,'FontSize',20)
set(alldatacursors,'FontName','Times')
set(alldatacursors,'FontWeight','bold')
set(alldatacursors,'Orientation','bottomright')
set(alldatacursors,'MarkerSize',10)
If you're generating the datatips programmatically, you can get the handles to the parent DataTip objects directly during creation, but as far as I can tell, we are prevented from directly accessing the handle to the child PointDataTip object without the roundabout search for it.
... at least that's what I've managed to figure out.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

产品


版本

R2008b

Community Treasure Hunt

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

Start Hunting!

Translated by