How to quickly update plotted texts in axes?

2 次查看(过去 30 天)
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)?

采纳的回答

Walter Roberson
Walter Roberson 2021-2-16
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 个评论
Walter Roberson
Walter Roberson 2021-2-16
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.

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by