how to save plot handle in an array and delete the plot later?
Mostrar comentarios más antiguos
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
Más respuestas (1)
Devanuj Deka
el 15 de Jul. de 2021
Editada: Devanuj Deka
el 15 de Jul. de 2021
1 voto
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
roudan
el 15 de Jul. de 2021
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.
roudan
el 15 de Jul. de 2021
It is not always necessary to hold on before creating multiple handles. For example,
h = plot(1:5, rand(5,7))
Furthermore, if you use primitive objects in the right modes, they do not check hold status:
h2 = line([1 5], [2 8])
h
h(end)
Observe that h stayed valid even though I drew more.
roudan
el 16 de Jul. de 2021
Devanuj Deka
el 16 de Jul. de 2021
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.
roudan
el 16 de Jul. de 2021
Devanuj Deka
el 16 de Jul. de 2021
You're welcome.
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
