how to save plot handle in an array and delete the plot later?

9 vues (au cours des 30 derniers jours)
roudan
roudan le 15 Juil 2021
Commenté : Devanuj Deka le 16 Juil 2021
Hi
In my plot, I have 10 plots. I am creating a 1D array using handle_array=zeros(10,1), then later I save ith plot's handle to the array using handle_array(i)=plot_handle. Later I'd like to delete the specific plot using delete(handle_array(ith)), and I got an error of annot access method 'delete' in class 'matlab.ui.Root'.
I found the data saved in the handle array is not a handle and it is just a number.
so how to save the plot handle and delete the plot later? Thanks for your help.

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Juil 2021
handle_array=zeros(10,1)
handle_array = 10×1
0 0 0 0 0 0 0 0 0 0
Do not do that for any modern version of matlab. Instead use
handle_array = gobjects(10,1)
handle_array =
10×1 GraphicsPlaceholder array: GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder
  3 commentaires
Walter Roberson
Walter Roberson le 15 Juil 2021
isvalid() or ishghandle() or ishandle()
ishandle() returns true for java objects as well as hg objects, but ishghandle() returns false for java.
isvalid() returns false for uninitialized or deleted handles.
roudan
roudan le 15 Juil 2021
Thank you Walter, ishandle() works perfectly! I appreicate your help. It saved me a lot of time! Have a great dat Sir!

Connectez-vous pour commenter.

Plus de réponses (1)

Devanuj Deka
Devanuj Deka le 15 Juil 2021
Modifié(e) : Devanuj Deka le 15 Juil 2021
I would suggest creating a cell array for handle_array. The function zeros(10,1) creates a 10x1 array of type 'double', which means you cannot store plot handles in it - it will store a numeric value instead of the plot handle.
With cell(sz1,...,szN) you can initialize an array with the same syntax as zeros(10,1), but you can store any data type in these individual cells.
  9 commentaires
roudan
roudan le 16 Juil 2021
Thanks Devanuj, yes I used (), using {} solved the problem. Thank you. I really appreciate your help.
Devanuj Deka
Devanuj Deka le 16 Juil 2021
You're welcome.

Connectez-vous pour commenter.

Catégories

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

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by