getpoints function is not saving variables outside of loop

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

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?
When I stop the program, they become 0x0 double vectors. However, when the program is running and I do not suppress the output, I get printed values like these:
timeLogs =
1.0e-04 *
Columns 1 through 10
0.0318 0.0692 0.0769 0.0834 0.0917 0.1020 0.1065 0.1124 0.1179 0.1251
Columns 11 through 20
0.1294 0.1362 0.1404 0.1446 0.1490 0.1531 0.1575 0.1622 0.1664 0.1713
Columns 21 through 30
0.1759 0.1800 0.1844 0.1911 0.1954 0.2007 0.2062 0.2110 0.2157 0.2206
Columns 31 through 40
0.2256 0.2299 0.2341 0.2392 0.2443 0.2494 0.2542 0.2584 0.2626 0.2668
Columns 41 through 46
0.2714 0.2765 0.2817 0.2863 0.2917 0.2969
ECGLogs =
Columns 1 through 10
1.1584 1.1584 1.1584 1.1584 1.1535 1.1535 1.1535 1.1535 1.1535 1.1535
Columns 11 through 20
1.1535 1.1486 1.1486 1.1486 1.1486 1.1486 1.1486 1.1486 1.1437 1.1437
Columns 21 through 30
1.1437 1.1437 1.1437 1.1437 1.1437 1.1437 1.1437 1.1388 1.1437 1.1388
Columns 31 through 40
1.1388 1.1388 1.1388 1.1388 1.1388 1.1388 1.1388 1.1388 1.1388 1.1388
Columns 41 through 46
1.1388 1.1388 1.1388 1.1388 1.1388 1.1388

Sign in to comment.

 Accepted Answer

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

Thank you so much! This worked, but is there anyway to save the data in the vectors without having to run the command outside of the for loop?
I am not sure why you are not able to save and retrieve the data as is, because when I tried the following code it works as you want it to. So I am not sure what is different in your setup. Note because I do not have sensor connected to arduino to test, I changed v = i in my for loop. But that should not affect the way animatedline works.
Can you think of anything that could be different? Is it possible that it is inside a function or something?
h = animatedline
ax = gca;
ax.YGrid = 'on';
ax.YLim = [0 2];
startTime = datetime('now');
for i= 1:500
% Read current voltage value
v = i;
[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
end
I just copied and pasted your code (and adjusted the v value as necessary), and I am still having the same issue. What version of MATLAB are you running? Because that is the only factor I can think of that may change the way the program is running.
I was using R2018a for all my tests. Sorry for the late response, for some reason I did not get notifications.

Sign in to comment.

More Answers (0)

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!