addpoints error "Value must be a handle"

75 次查看(过去 30 天)
Aim: Continuously read & plot data acquired from Arduino Uno + pressure transducer setup using "animatedline". After the experiment is done, hit "STOP" button on GUI, and gather data collected using "getpoints" to then be processed as necessary.
Problem: When I run the code, I am able to plot live signal from my transducer using "animatedline" (illustrated below).
The code that enables this is the following:
figure
an = animatedline('Marker','.','Color','r','LineWidth',1.5);
ax = gca; box on
ax.XGrid = 'on'; ax.YGrid = 'on';
ax.YLim = [0 32]; % psi
ButtonHandle = uicontrol('Style', 'PushButton', ...
'String', 'STOP', ...
'BackgroundColor','r',...
'Callback', 'exit_loop');
startTime = datetime('now');
eject = 0;
while t < 10
voltage = readVoltage(a,'A0');
maxP = 30; minP = 0; % Dictated by transducer
pOffset = 7.44; % Observed voltage correction
pressure = voltage*((maxP-minP)/(5-1))-pOffset; % Convert signal to pressure
t = datetime('now') - startTime;
addpoints(an,datenum(t),pressure) % adds points to plot
ax.XLim = datenum([t-seconds(15) t]); % updates the x-axis
datetick('x','keeplimits') % change x-axis to time label
drawnow limitrate
end
The code for the "STOP" button in the bottom left corner is below. I have this saved as a separate script, but in the same folder:
function exit_loop(hObject, eventdata, handles)
selection = questdlg('Sure you want to exit?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection
case 'Yes'
delete(gcf)
case 'No'
return
end
Once I hit "Yes," however, I then get this error:
Error using matlab.graphics.animation.AnimatedLine/addpoints
Value must be a handle.
Error in pRecord_v0 (line 117)
addpoints(an,datenum(t),pressure) % adds
points to plot
Same error when I try to store the data from the experiment using the following:
[timeLog,pLog] = getpoints(an);
Any help you can offer is much appreciated!

采纳的回答

Steven Lord
Steven Lord 2020-7-22
Guard your addpoints call with a check to make sure the animatedline handle still isvalid.
L = animatedline;
axis([0 360 -1 1]);
for d = 0:360
if isvalid(L)
addpoints(L, d, sind(d));
else
break % stop trying to add points
end
if d == 180
cla % get rid of the animatedline
end
drawnow expose
end
disp(d) % 181
  3 个评论
Steven Lord
Steven Lord 2020-7-23
Let's say you're writing down numbers in a notebook once an hour. Then between writing two numbers, someone throws your notebook into a paper shredder. When you have to record the next number you write the number on the table, look around for your notebook, and get confused when you can't find it.
The notebook is the animatedline. addpoints represents writing down the numbers. Closing the figure in which the animatedline "lives" is like throwing it in the shredder. When MATLAB gets confused it throws an error.
Calling isvalid is you specifically checking if the notebook exists (before trying to write down the number with addpoints.) Stopping the loop with break represents you giving up on writing down the numbers because you've learned the notebook no longer exists.
Luke G.
Luke G. 2020-7-23
I see. So when you don't check if the notebook is still there, MATLAB freaks out because it wants to write (i.e., addpoints) in the notebook (i.e., animatedline) and not on the table. So this is basically saying "If the notebook is still on the table, write down the recorded data. Else, stop trying to write down data."
That's a great analogy, and I think I understand it now. Many thanks Steven!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by