addpoints error "Value must be a handle"
41 views (last 30 days)
Show older comments
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/335637/image.png)
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!
0 Comments
Accepted Answer
Steven Lord
on 22 Jul 2020
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 Comments
Steven Lord
on 23 Jul 2020
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.
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!