MATLAB: How to obtain real time analog data and store it in an array

analog inputarduinoserial communication

I have a potentiometer through which I need to take in inputs continuously and store in an array so that I plot it later. I take in inputs through Arduino Uno which I have successfully interfaced with MATLAB. I can obtain the voltage value at a given instant. But I need to store the value at each time into an array. Can anyone suggest any method to do that? Thanks
I use MATLAB 2014a version.
Please excuse if it is a simple/silly question. I am just a beginner.

Best Answer

Srinidhi - how many of these voltages to you expect to acquire? if you need to store the value in an array, then you can do so by just creating the array ahead of time (outside your for or while loop) and then update that array with the voltage data. Consider the following
% create an array of 10000 elements to store the voltage data
voltageData = zeros(10000,1);
% track the current voltage index
voltageIdx = 0;
% communicate with Arduino
while true
% get the voltage data
voltValue = ...;
% save it to the array
voltageIdx = voltageIdx + 1;
voltageData(voltageIdx) = voltValue;
end
Without knowing exactly how your code is written, you can try the above which will allow you to collect the voltage data over time.