getpoints function is not saving variables outside of loop
2 views (last 30 days)
Show older comments
Gabrielle Mills
on 13 Apr 2018
Commented: Madhu Govindarajan
on 26 Apr 2018
I am attempting to edit an existing code that will read ECG data from an Arduino Uno (in COM3), display the live ECG on a plot, and save the recorded data points. I am able to read the data and display it live, but the variables with the ECG data and time points do not save outside of the "for" loop. I think this may be an issue with the getpoints function, but any advice or input on how to save the variables ECGLogs and timeLogs would be greatly appreciated!
Here's the code I have:
ECG = arduino('COM3');
%%Acquire and display live data
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [0 2];
startTime = datetime('now');
for i= 1:500
% Read current voltage value
v = readVoltage(ECG,'A0');
[timeLogs,ECGLogs] = getpoints(h);
f = [timeLogs,ECGLogs];
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h,datenum(t),v);
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
% Check stop condition
stop = readDigitalPin(ECG,'D12');
i = i+1;
end
2 Comments
David Fletcher
on 13 Apr 2018
Effectively on each iteration of the loop you are throwing away the previous values of timeLogs and ECGlogs by overwriting f with an array of the new values. What is the size of of timelogs and ECGlogs - are they scaler values?
Accepted Answer
Madhu Govindarajan
on 17 Apr 2018
Once you have ran the for loop, without closing the figure try running the following command outside of the for loop.
[timeLogs,ECGLogs] = getpoints(h);
This should give you the datapoints from the animatedline.
Also inside the for loop, you don't need i = i+1;
4 Comments
Madhu Govindarajan
on 26 Apr 2018
I was using R2018a for all my tests. Sorry for the late response, for some reason I did not get notifications.
More Answers (0)
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!