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

200 Ansichten (letzte 30 Tage)
Hans123
Hans123 am 9 Mai 2019
Kommentiert: Hans123 am 13 Mai 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;
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 Kommentar
Hans123
Hans123 am 9 Mai 2019
I think the handle is overwritten, I updated the question and pasted my original code

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 9 Mai 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.
  3 Kommentare
Adam Danz
Adam Danz am 9 Mai 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
Hans123 am 13 Mai 2019
thanks for showing me your thought process, really appreciate it!
cheers

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by