0

I am now writing a code to display and remove a text object from an axes. However, I do not know the way to redisplay the same text which is deleted. You may understand better by the following codes:

I can add a text to my axes by the following code: textHandle=text(1,1,'Text')

I also know I can delete the text 'Text' by: delete(textHandle)

However, how to redisplay the textHandle again at the axes? Thank you for your kind attention and help.

Myrick Chow
  • 332
  • 1
  • 6
  • 16
  • 2
    if you used `delete(handle)`, the object is destroyed and no recovery is possible. You have to re-create it (as you created it in the first place). If you do not want to delete it but simply hide it temporarily, then look at the `visible` properties of the [`text`](http://www.mathworks.co.uk/help/matlab/ref/text-properties.html?searchHighlight=text) object. – Hoki Oct 04 '14 at 09:40
  • Thnak you very much Hoki! – Myrick Chow Oct 04 '14 at 09:42
  • @Hoki Make that comment an answer? It seems good enough – Luis Mendo Oct 04 '14 at 16:22
  • @LuisMendo. yep, done. Thanks. I developed it a bit to make it more complete. I am still unsure sometimes about what should be an _answer_ and what is just worth a _comment_, mainly in cases like this where the answer is very short or simple. – Hoki Oct 04 '14 at 17:55
  • @Hoki I am often unsure too. But the comment seemed to be appropriate for the OP and I think it makes a good answer. +1 BTW – Luis Mendo Oct 04 '14 at 19:36

1 Answers1

1

if you use delete(ObjectHandle), the object is destroyed and no recovery is possible. You just have to re-create it the same way you created it in the first place.

Note that this apply for a text object but also for any type of Matlab object.


If you do not want to delete it, but simply hide it temporarily until you reuse it, then use the visible property of the text object.

For example:

set(textHandle,'Visible','off')

will simply make the text object invisible. When you want to make it reappear, switch the property back to 'visible' :

set(textHandle,'Visible','on')

Obviously, this method is only useful if you are sure to reuse your object later on.

Besides the (very small) performance gain (not significant for a single text object, but can be useful if many text objects are to be hidden), the main advantage of doing it this way is that you can still call and modify you text object even when it is hidden. For example:

set(textHandle,'String','New updated text')

will execute fine and will display the 'New updated text' when the visibility of the text object is restored.

Had you try to set this property after you deleted the object, Matlab would have just be angry at you and send you back the classic error ??? Error using ==> set / Invalid handle object.

Hoki
  • 11,637
  • 1
  • 24
  • 43