Clear Filters
Clear Filters

Error using matlab.ui.​control.UI​Control/ge​t Invalid or deleted object inside a while loop

18 views (last 30 days)
I click on the plot data in an axes window all goes fine, except when i finish/end the gui this error occurs.
Error using matlab.ui.control.UIControl/get
Invalid or deleted object.
Error in BSAKit>pbtECG_Callback (line 207)
while get(hObject,'Value')
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in BSAKit (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)BSAKit('pbtECG_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating DestroyedObject Callback.
Code:
function pbtECG_Callback(hObject, eventdata, handles)
% hObject handle to pbtECG (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
global ecgdata;
warning off
yMax = 1.5; %y Maximum Value
yMin = -0.5; %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set x-min
max = 5; % set x-max
delay = .001; % make sure sample faster than resolution
t = 0;
e1 = 0;
e2 = 0;
e3 = 0;
plotTitle = 'ECG plot'; % plot title
xLabel = 'Time (s)'; % x-axis label
yLabel = 'ECG (V)'; % y-axis label
legend1 = 'Lead 1';
legend2 = 'Lead 2';
legend3 = 'Lead 3';
switch get(hObject,'Value')
case 0
set(hObject,'String','Start')
fprintf(handles.serConn, 'e');
case 1
set(hObject,'String','Stop')
set(handles.serConn, 'TImeout',2);
axes(handles.axeChannel1);
cla;
legend(legend1);
axis([min max yMin yMax]);
axis 'auto y' % moi them vao
grid(plotGrid);
hold on
li1=line(NaN,NaN);
axes(handles.axeChannel2);
cla;
legend(legend2);
axis([min max yMin yMax]);
axis 'auto y'
grid(plotGrid);
hold on
li2=line(NaN,NaN);
axes(handles.axeChannel3);
cla;
legend(legend3);
axis([min max yMin yMax]);
axis 'auto y'
grid(plotGrid);
hold on
li3=line(NaN,NaN);
fprintf(handles.serConn, 'E');
ei=0;
ecgdata=[0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0];
end
tic
time1=0;
time2=0;
ecg1=[0];
ecg2=[0];
ecg3=[0];
x=[0];
while get(hObject,'Value')
data = fscanf(handles.serConn, '%s');
%count=100;
%maxloop=2;
%while((count>50)&&(maxloop>0))
% [data,count] = fscanf(handles.serConn, '%s');
% maxloop=maxloop-1;
%end
sa = str2num(data);
if(size(sa,2)>11)
ei=ei+1;
%ecgdata(ei,:)=sa;
ecgdata(ei,:)=[sa(1) 0.04185.*sa(2:9)-685.71 sa(10:12)];
if(get(handles.chkPlotECG,'Value'))
e1=sa(2);
e1=0.04185*e1-685.71;%1000*((e1/32768)-1/2)*4.8/3.5;
e2=sa(3);
e2=0.04185*e2-685.71;%1000*((e2/32768)-1/2)*2*2.4/3.5;
e3=e1-e2;
temp=sa(1)/1000;
x=[x temp];
ecg1 = [ecg1 e1];
ecg2 = [ecg2 e2];
ecg3 = [ecg3 e3];
if(temp-time1>=0.1)
time1=time2;
time2=temp;
set(li1,'XData',[get(li1,'XData') x], 'YData',[get(li1,'YData') ecg1]);
set(li2,'XData',[get(li2,'XData') x], 'YData',[get(li2,'YData') ecg2]);
set(li3,'XData',[get(li3,'XData') x], 'YData',[get(li3,'YData') ecg3]);
x=[];
ecg1=[];
ecg2=[];
ecg3=[];
if(time2>max)
set(handles.axeChannel1,'XLim',[(time2)-max time2]);
set(handles.axeChannel2,'XLim',[(time2)-max time2]);
set(handles.axeChannel3,'XLim',[(time2)-max time2]);
end
end
end
end
t=t+1;
pause(delay);
end
  2 Comments
Rik
Rik on 19 Sep 2022
'when you're finished'
Did you close the figure window? How did you make sure the while loop exists properly before the object this callback refers to is destroyed?
Walter Roberson
Walter Roberson on 19 Sep 2022
Exactly. When you close the figure window, the object stops existing. You should
while isvalid(hObject) && get(hObject,'Value')

Sign in to comment.

Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!