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

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.

 Respuesta aceptada

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 comentarios

Thanks Walter. so this way the handle_array is not empty initially, right? Later how do I check if the handle exits or not? if exist, I will delete the plot. How to do it? Thanks
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.
Thank you Walter, ishandle() works perfectly! I appreicate your help. It saved me a lot of time! Have a great dat Sir!

Iniciar sesión para comentar.

Más respuestas (1)

Devanuj Deka
Devanuj Deka el 15 de Jul. de 2021
Editada: Devanuj Deka el 15 de Jul. de 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 comentarios

Thanks Devannuj, after saving handle to cell, how do I check if handle exists? Thanks
Devanuj Deka
Devanuj Deka el 15 de Jul. de 2021
Editada: Devanuj Deka el 15 de Jul. de 2021
You can access the i'th handle by running
handle_array{i}
It seems that for multiple handles to exist, we need to use
hold on;
before plotting the lines and storing the handles. This way each line will be added to the same graph without overwriting the previous lines, and the handles will keep existing unless you either delete that handle or overwrite the plot with a new line.
Hi Devannuj, it didn't work, the error is: Conversion to cell from matlab.graphics.chart.primitive.Line is not possible., thanks for your help
It is not always necessary to hold on before creating multiple handles. For example,
h = plot(1:5, rand(5,7))
h =
7×1 Line array: Line Line Line Line Line Line Line
Furthermore, if you use primitive objects in the right modes, they do not check hold status:
h2 = line([1 5], [2 8])
h2 =
Line with properties: Color: [0 0.4470 0.7410] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 5] YData: [2 8] ZData: [1×0 double] Show all properties
h
h =
7×1 Line array: Line Line Line Line Line Line Line
h(end)
ans =
Line with properties: Color: [0.6350 0.0780 0.1840] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 2 3 4 5] YData: [0.5669 0.9947 0.1465 0.6838 0.6228] ZData: [1×0 double] Show all properties
Observe that h stayed valid even though I drew more.
Thank you Devanuj and Walter. I appreicate your help,
Devanuj Deka
Devanuj Deka el 16 de Jul. de 2021
Editada: Devanuj Deka el 16 de Jul. de 2021
@roudan, the reason you're getting this error "Conversion to cell from matlab.graphics.chart.primitive.Line is not possible.", is that you're assigning the plot handle to the cell array incorrectly (i.e. using parantheses).
handle_array(i) = plot(_,_); % This will give you the above mentioned error.
Assignment of an element to a cell is done using curly brackets:
handle_array{i} = plot(_,_); % This will store the plot handle inside the i'th cell in handle_array.
Hope this helps.
Thanks Devanuj, yes I used (), using {} solved the problem. Thank you. I really appreciate your help.
You're welcome.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 15 de Jul. de 2021

Comentada:

el 16 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by