TextScatter implementation does not expose the indices it chose for display when 'TextDensityPercentage' is used

rng(10) % for reproducibility
n=100;
x = randn(n,1);
y = randn(n,1);
seq = (1:n)';
h=textscatter(x,y,string(seq), ...
'TextDensityPercentage',20, ...
'DisplayName','text')
I want to know the indexes of the unis which have been displayed by function textscatter from handle h (in this case 1, 4 and 22) or from findall, ancestor, ....
I wanted to use textscatter with geoaxes but it seems it is not supported. Therefore the idea is to project the map on cartesian axes and then retrieve the indexes of displayed units from textscatter with 'Name', Value 'TextDensityPercentage',20, but I need to know the indexes.
Any other idea is welcome (apart from "implementing my own label-selection rule" which labels points if they are not closer than a threshold) .
Copilot and ChatGPT claim it is not possible but I want to have your opinion.
Thank you in advance

Answers (1)

Hi Marco,
You can give a try on recovering the indices via label text, since your labels are the indices themselves, you can
  • Extract the displayed text strings
  • Convert them back to numbers
  • Those numbers are the indices
Please refer to the code section for implementation.
txtHandles = findall(h, 'Type', 'Text');
displayedLabels = string({txtHandles.String});
displayedIdx = str2double(displayedLabels);
displayedIdx = sort(displayedIdx);

2 Comments

Dear Yashwanth thank you very much.
this is indeed the first solution we tried and it works witht the labels created by function text but does not work with the labels produced by textscatter
Thanks again
In other words
rng(10) % for reproducibility
n=100;
x = randn(n,1);
y = randn(n,1);
seq = (1:n)';
h=textscatter(x,y,string(seq), ...
'TextDensityPercentage',20, ...
'DisplayName','text');
txtHandles = findall(h, 'Type', 'Text');
% txtHandles is empty
disp(txtHandles)
0×0 empty GraphicsPlaceholder array.

Sign in to comment.

Categories

Find more on Phased Array Design and Analysis in Help Center and File Exchange

Products

Release

R2025b

Asked:

on 7 Jan 2026

Commented:

on 17 Jan 2026

Community Treasure Hunt

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

Start Hunting!