MATLAB: Storing Live streamed data

storing

Hi, so i am currently working on a project which is taking streamed data from a device to matlab. Now i am using this data to control a prosthesis but i am struggling to write some code that will store the data in an appropriate variable that can run classification (Machine learning) on the data. I have 4 channels worth of data, and the easiest part of this is i dont even need all the data saved, just the most significant feature i.e. only above a threshold.
The streamed data is coming in at a rate of 2000Hz but as i said before i only need the values after the thresholding. I simply am struggling with storing this data, the threhsold is of course easy. I dont know the size of the data to create as its live data which is a problem in itself (Not being able to run machine learning on the whole data).
I have been trying this for a while, if anyone can help it would be greatly appreciated.

Best Answer

Open a file before beginning the process and write whatever it is that is wanted...formatted data is simple to read but bulky, stream data is compact and the fastest way...not having been given any of your code variables can't try to fit into whatever it is you do have, but the idea is
fid=fopen('LoggingFile.dat','w');
collecting=true;
while collecting
x=readdatafromsource;
y=threshold(x);
fwrite(fid,y,'double')
end
fid=fclose(fid);
The above presumes some other manner such as a pushbutton callback that sets the variable collecting to false to stop the collection process; you've apparently got all that worked out already.
Then just
fid=fopen('LoggingFile.dat','r');
data=fread(fid);
fid=fclose(fid);
will give you the saved data in a 2D array, assuming the x data are a 4-vector as described for each collection cycle.
ADDENDUM:
Alternatively, if you have sufficent memory, hold the data in memory and use a .mat file at the end.
Related Question