JimHokanson / labchart_server_matlab

Communicating with Labchart in Matlab
MIT License
11 stars 2 forks source link

retrieve the latest data after doing something #3

Open JimHokanson opened 4 months ago

JimHokanson commented 4 months ago

Someone emailed me asking: "each time I run a command in MATLAB, I want to get the current value from a specific channel in LabChart. What command should I use to achieve this with low latency?"

Here's my response. I should note I have not tested this yet ....

chan_number = 1; %Adjust as needed
doc = labchart.getActiveDocument();

%You can query where you are at using the following:
%This may be -1 if not recording
block_number = doc.current_record;
end_time = doc.getRecordLengthInSeconds(block_number);

%This will retrieve the last 10 seconds of data, adjust as desired
start_time = end_time - 10
[data,time] = doc.getChannelData(chan_number,block_number,start_time,end_time,'as_time',true)

%If you want to do this in samples:
%-----------------------------------------------
end_sample = doc.getRecordLengthInTicks(block_number);
%Grab 5 samples in total
start_sample = end_sample - 5 + 1; %add 1 so otherwise you'll get 6 samples
data = doc.getChannelData('my_channel',block_number,start_sample,end_sample);

Note, both of these examples ASSUMES that you can go back that far. To be a bit more robust you would check that your start is > 0

guojinhuei commented 4 months ago

I tested the code using the method "%If you want to do this in samples:" and it works. Thank you for your help. This code is very useful for my graduate thesis and research.

However, I have a question. Why does the data size differ each time I run the code? For example, I expect to "grab 5 samples in total," but sometimes it retrieves 105 samples.

I think this might be because "LabChart supports streaming via an event called OnNewSamples. This event is not channel specific. It gets called maybe every 50 ms (need to check this)," as you mentioned in https://github.com/JimHokanson/labchart_server_matlab/blob/master/documentation/streaming.md. There may be a time difference between requesting end_sample or nTicks and obtaining the actual data.

The delay between data acquisition and generation seems sufficient for my current research, but I am not sure of the exact delay. However, this might not be a critical issue.

Once again, thank you very much for your code on GitHub.