How can I stop getting "Invalid or deleted object" error?
Mostrar comentarios más antiguos
Hello, I know this error has been discussed in many questions but I can't seem to find one that solves my specific doubt. I'm have a table with data. When you press on any cell it selects the whole row and then it plots an associated point in a uiaxes plot that already has something plotted in it and which I want to keep there, like a locked background. What I'm trying to do is to delete each point and plot a new one when a different row is selected without modifying the background. This works fine with the following code when selecting individual rows:
function UITableCellSelection(app, event)
app.SelectedRows = [];
delete(app.plotOptimum)
cols = size(app.tableData,2);
rows = event.Indices(:,1);
j = 1;
k = 1;
while j<=length(rows) % Selected rows gives something like [1 1 1 2 2 2 3 3 3]. This while transforms it into [1 2 3]
app.SelectedRows(k) = rows(j);
k = k+1;
j = j+cols;
end
mn = app.tableData(:,3); % Vector with X coordinates
b_mn = app.tableData(:,4)./mn; % Vector with Y coordinates
for j = [1:length(app.SelectedRows)]
app.plotOptimum(j) = plot(app.UIAxes2,mn(app.SelectedRows(j)),b_mn(app.SelectedRows(j)),'Marker','o','MarkerFaceColor','r','MarkerEdgeColor','r','MarkerSize',10);
end
end
What I'm doing is saving the plot handle into an app property so that I don't get an "undefined variable plotOptimum" when I try to delete it at the beginning. Idk why but after I select more than one row I get an "Invalid or deleted object" error if I try to select other rows afterwards. If you could help me discover why this happens or if you could give me an idea on how to take a different approach to achieve the same it would be greatly appreciated :)
Respuesta aceptada
Más respuestas (1)
What about checking, if the objects are existing, before deleting?
% delete(app.plotOptimum) ==>
delete(app.plotOptimum(isgraphics(app.plotOptimum))) % [EDITED]
Good programming practice:
Matlab processes FOR loops efficiently, if you provide the initial and final element:
for j = 1:length(app.SelectedRows)
Then the vector of the indices is not created explicitely. With your code:
for j = [1:length(app.SelectedRows)]
internally this is performed:
v = [1:length(app.SelectedRows)];
for k = 1:numel(v)
k = v(k);
...
end
This is more expensive. So omit the square brackets around the index vector. Remember that [] is Matlab operator for an array concatenation and here you concatenate the index vector with nothing.
2 comentarios
Radu Andrei Matei
el 14 de Jun. de 2022
Jan
el 15 de Jun. de 2022
I meant:
delete(app.plotOptimum(isgraphics(app.plotOptimum)))
Categorías
Más información sobre Graphics Performance 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!