Clear Filters
Clear Filters

How to quickly update plotted texts in axes?

2 views (last 30 days)
I am plotting a fairly big amount of patches (100+) together with their 'ids' as strings, in a loop to create an animation.
I am able to update the patches using the set command, since they are all contained in a 1x1 Patch object. I am not able to find something as straight forward for the texts, because they are in a 100x1 array of Text objects.
With a 100x1 array of Text-objects, can I somehow do an array-based update similar to how I can do for my patches (without having to re-plot or loop over each text object)?

Accepted Answer

Walter Roberson
Walter Roberson on 16 Feb 2021
Yes, it is possible, but not well known. I end up having to go back to the reference material every time I try to use it.
t(1) = text(0,0.5,'string1');
t(2) = text(.3,0.5,'string2');
set(t,{'string'},{'test1';'test2'}) %notice the column vector of new values
  3 Comments
Walter Roberson
Walter Roberson on 16 Feb 2021
Indirectly. text() has Position, which is a vector of [x y z]. To update the x separately you need to fetch the original data, update the x, and store back the data.
t(1) = text(0,0.5,'string1');
t(2) = text(.3,0.5,'string2');
set(t,{'string'},{'test1';'test2'}) %notice the column vector of new values
P = cell2mat(get(t, 'Position'));
P(:,1) = [0.2; 0.6]; %vector of new x data
P = num2cell(P,2);
set(t, {'Position'}, P)
The get(), transfer to/from cell, and set(), could be simplified to a simple set() if you are changing both x and y data at the same time; just remember to toss a z coordinate (0 if appropriate) in at the same time.
The num2cell() trick might come in handy for you if you have an array of coordinates.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!