Update multiple objects Position(2) at once

2 vues (au cours des 30 derniers jours)
L Oc
L Oc le 17 Jan 2025
Commenté : L Oc le 20 Jan 2025
Hello,
I want to update at once all the Position(2) of some graphic objects that are on different tiles of a same figure.
I am not sure what would be the best way - I assume using a loop would work but is not ideal. I wish I could use the linkprop function, but so far I have been able to use it only to link the complete position, instead of linking only the second element of Position.
I also have tried to use arrayfun to edit only the second element of position, but my objects are stored in a graphic array that has empty elements bacause there are not the same number of object per tile.
Here is a simplified example of my problem. I would like to change all the T.Position(2) at once:
% preallocate arrays to store my future objects
T = gobjects(3,4);
ax = gobjects(1,3);
f = figure(1);
t = tiledlayout(3,1);
for i = 1:3
ax(i) = nexttile; hold on;
plot([0 10],[1 5]);
for j = 1:i
T(i,j) = text(ax(i), j*2, 1, 'X'); % <-- objects which position needs to be updated later
end
end
Any thoughts or ideas are welcome, including suggestions concerning how to store my objects in a better way! Thank you!

Réponse acceptée

Rik
Rik le 17 Jan 2025
A loop is actually the best way. Using arrayfun will only disguise the loop and cost you performance. Interestingly, cellfun with char inputs is faster than a for loop, but with a function handle (or anonymous function) is slower than a loop.
It would have been nicer to link the properties, but unfortunately, you can't link only part of a property.
What you might try (I haven't tested this), is to use panels instead of tiles. That way the Position property is relative to the panel (since that is the parent), which means the relative position is the same. This still wouldn't solve the issue of one axes containing more objects than another one.
  1 commentaire
L Oc
L Oc le 20 Jan 2025
Thank you for your clear explanations! I ended up making a loop that works just fine, even if I am a little frustrated not to have a more elegant solution :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by