Why am I getting the error "Invalid or Deleted Object"

조회 수: 200 (최근 30일)
Hans123
Hans123 2019년 5월 9일
댓글: Hans123 2019년 5월 13일
This is my code
%constants A and B calculations
y1 = Mean(1);
x1 = Mean_step(1);
initial = y1*x1;
y2 = Mean(2);
x2 = Mean_step(2);
constant_B = (x2*y2-initial)/(y1-y2);
constant_A=(initial)+((y1)*constant_B);
%model calculation
Model = constant_A./(Mean_step + constant_B);
hp = plot(Mean_step, Model, 'r-', 'Linewidth', 1.5);
%%%dynamic text
txt1 = ['V_{preamp} = ' num2str(constant_A) ' / (Tool-Substrate Gap + (' num2str(constant_B) '))'];
ht = text(400, 650, txt1, 'FontSize', 9);
txt2 = ['Current V_{preamp} value = ' num2str(Mean(k)) '| Current Z-piezo distance = (' num2str(Mean_step(k)) '))'];
ht2 = text(400, 600, txt2, 'FontSize', 9);
txt3 =['Current Z-piezo distance = ' num2str(Mean_step(k)) ''];
ht3 = text(400, 550, txt3, 'FontSize', 9);
txt4 = ['Percentage Change = ' num2str(percent_change)];
ht4 = text(400 , 500, txt4, 'FontSize', 9);
%animated line handle, the line after the trailing dot
hh=animatedline('Marker','o','MarkerSize', 3,'MarkerFaceColor',[0.2 0.2 0.2]);
%trailing dot handle
h = plot(nan, nan, 'yo', 'MarkerSize', 5, 'MarkerFaceColor', 'g','MarkerEdgeColor','g'); %yellow, filled, large
for k = 2:length(Mean)
y2=Mean(k);
x2=Mean_step(k);
constant_B=(x2*y2-initial)/(y1-y2);
constant_A=(initial)+((y1)*constant_B);
Model = constant_A./(Mean_step + constant_B);
hp.YData = Model;
txt1 = ['V_{preamp} = ' num2str(constant_A) ' / (Tool-Substrate Gap + (' num2str(constant_B) '))'];
ht.String = txt1;
addpoints(hh, Mean_step(k), Mean(k))
txt2 = ['Current V_{preamp} value = ' num2str(Mean(k)) ' V'];
ht2.String = txt2;
txt3 =['Current Z-piezo distance = ' num2str(Mean_step(k)) ' \mum'];
ht3.String = txt3;
txt4 = ['Percentage Change = ' num2str(percent_change)];
ht4.String = txt4;
set(h, 'XData', Mean_step(k), 'YData', Mean(k));
drawnow()
end
  댓글 수: 1
Hans123
Hans123 2019년 5월 9일
I think the handle is overwritten, I updated the question and pasted my original code

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 5월 9일
Problem
First you produce a plot and assign the handle to hp
hp = plot(Mean_step, Model, 'r-', 'Linewidth', 1.5);
Then you destroy that plot and overwrite it with this line.
h = plot(nan, nan, 'yo', 'MarkerSize', 5, 'MarkerFaceColor', 'g','MarkerEdgeColor','g'); %yellow, filled, large
When you get to this line of code (below) hp is no longer a valid object since it was deleted.
hp.YData = Model;
Solution
Option 1: hold on.
hp = plot(Mean_step, Model, 'r-', 'Linewidth', 1.5);
hold on %now the 2nd line will be added to the plot
Option 2: new figure
figure()
h = plot(nan, nan, 'yo', 'MarkerSize', 5, 'MarkerFaceColor', 'g','MarkerEdgeColor','g'); %yellow, filled, large
% now the two lines will be on two different figures.
  댓글 수: 3
Adam Danz
Adam Danz 2019년 5월 9일
Glad I could help. I knew to look for one of these:
  • delete(obj)
  • clear(obj)
  • more than 1 plot without a "hold on" <--- this was the culprit
  • Accessing an undefined variable <---- could have been this (we don't have values for several of your variables).
Hans123
Hans123 2019년 5월 13일
thanks for showing me your thought process, really appreciate it!
cheers

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by