Update multiple objects Position(2) at once

조회 수: 3 (최근 30일)
L Oc
L Oc 2025년 1월 17일
댓글: L Oc 2025년 1월 20일
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!

채택된 답변

Rik
Rik 2025년 1월 17일
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
L Oc
L Oc 2025년 1월 20일
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 :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by