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

128 次查看(过去 30 天)
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 :)

采纳的回答

Radu Andrei Matei
Radu Andrei Matei 2022-6-15
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.

更多回答(1 个)

Jan
Jan 2022-6-12
编辑:Jan 2022-6-15
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 个评论
Radu Andrei Matei
Radu Andrei Matei 2022-6-14
Hi, thank you for your answer. Unfortunately it doesn't work.
I don't know if this line:
delete(app.plotOptimum(isgraphics(delete(app.plotOptimum))))
is meant to mean:
if isgraphics(app.plotOptimum)
delete(app.plotOptimum)
end
What I've realised is that whenever I try to delete an array Matlab gives me an "Invalid or deleted object" and I think it's because of the "invalid" part. This is why when I select multiple rows and it plots multiple points while saving each graphics object as an element of the array app.plotOptimum it throws the error. I don't know what to do about it. If you could me give any ideas I would really appreciate it :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by