Hoe to implement a listener that deletes its calling object?

1 回表示 (過去 30 日間)
royk
royk 2020 年 7 月 12 日
コメント済み: Walter Roberson 2020 年 7 月 17 日
I would like to trigger DELETE of an IMAGES.ROI object upon ROIMoved event.
I am trying:
figure(1);clf;axis([0 100 0 100])
h = images.roi.Rectangle(gca,'position',uint16([10 20 10 20]));
addlistener(h,'ROIMoved',@(s,~)delete(s))
when the rectangle is moved it gets deleted (as desired) but it also issues an error:
Error using images.roi.Rectangle/MarkDirty
Value must be a handle.
Error in images.roi.internal.ROI/stopDrag
Error in images.roi.internal.ROI
What would be the right way for a listener to delete its object without invoking an error?

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 12 日
編集済み: Walter Roberson 2020 年 7 月 12 日
The right way would probably be not to do that at all.
Instead, put in a position constraint that prevents the ROI from moving and use a different method to delete the ROI if it must be deleted. In particular, do not delete the ROI from within a listener to the ROI (but you could delete the ROI from within a listener to mouse presses or key presses.)
  5 件のコメント
royk
royk 2020 年 7 月 12 日
i guess... but am i not running into the same problem vis a vis deleting the timer (I guess the function called by the timer will have to delete the timer - is that allowed?)
Walter Roberson
Walter Roberson 2020 年 7 月 17 日
Yes, the callback function can delete the timer. But if you are doing this a bunch of time, it would be more efficient to just keep one timer around and change its callback (or properties), configure it to fire once, and start it as needed.

サインインしてコメントする。

その他の回答 (1 件)

royk
royk 2020 年 7 月 12 日
thanks Walter
What I want to do is is to specifically delete the ROI only after it is moved (the move is important becuase i want to record the new position of the ROI as an input before deleting it). Is there no way to delete an ROI based on its ROIMoved event?

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by