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.
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 :)
댓글 수: 0
채택된 답변
추가 답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!