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

223 views (last 30 days)
Hans123 on 9 May 2019
Commented: Hans123 on 13 May 2019
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;
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 Comment

Hans123 on 9 May 2019
I think the handle is overwritten, I updated the question and pasted my original code

Adam Danz on 9 May 2019
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.

Hans123 on 9 May 2019
Ah.... that solves everything, thank you.
Adam Danz on 9 May 2019
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 on 13 May 2019
thanks for showing me your thought process, really appreciate it!
cheers