How to quickly update plotted texts in axes?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andreas Nord
am 16 Feb. 2021
Kommentiert: Andreas Nord
am 16 Feb. 2021
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)?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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 Kommentare
Walter Roberson
am 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!