How can I stop getting "Invalid or deleted object" error?

32 views (last 30 days)
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 :)

Accepted Answer

Radu Andrei Matei
Radu Andrei Matei on 15 Jun 2022
I've solved it. I have a population of points that I wanted to to be drawn on a plot whener I selected them and to be automatically deleted and substituted by another point whenever I selected it. What I did is draw all the points, set their visibility off and then set it on again whenever selected, making sure to set everything off again before drawing the new one and it works as I wanted it.

More Answers (1)

Jan
Jan on 12 Jun 2022
Edited: Jan on 15 Jun 2022
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 Comments
Jan
Jan on 15 Jun 2022
I meant:
delete(app.plotOptimum(isgraphics(app.plotOptimum)))

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!