I want to use the Datalogger Matlab API to stream and plot data for some duration of time. I set the duration to 12 seconds, for example. At sampling rates lower than 10^4 the instrument cannot reach the desired duration of streaming time and stops at ~8.3 s in case of 100 Sa/s and at ~11.3 s in case of 1,000 Sa/s. Only when the sampling rate is 10,000 Sa/s and higher, the scan duration reaches the target of 12 s. I can in principle use higher sampling rates and average the data (as shown in my code, attached), but that’s a waste of time. It seems like at lower sampling rates everything should be smoother and faster, but that’s not the case.
MokuGo_datalogger_test.m (1.3 KB)
Hello,
I believe the issue is that your while loop is stopping early. Please refer to this GitHub example (moku-examples/matlab-api/datalogger_streaming.matlab at main · liquidinstruments/moku-examples · GitHub) and follow the same structure. This should fix your issue.
Yes, I had modified that example to create my own code. But the example itself is resulting in an error message due to not being able to close the while loop:
Error using MokuStreamInstrument/get_stream_data (line 130)
End of stream
Error in MokuGo_datalogger_test4 (line 27)
data = i.get_stream_data();
I was able to force the code to work (see attached), but it is obvious that there is some issue during data streaming. The problem is that the Matlab instrument for datalogger is “stuttering” during data streaming. At 100 Hz samling rate the “stuttering” period is ~8 s. I can see the same issue with the example code as well.
MokuGo_datalogger_test.m (1.7 KB)
Hello,
The way that data streaming works on Moku is that chunks of data are collected and then they are displayed. This is why it seems to be “stuttering” at 8 seconds; the Moku is in the process of collecting more data. The error message you are seeing isn’t necessarily an error, it is more an exception. The script has reached the end of the stream so it spits out an error but it is simply due to streaming ending. In your original code, it wasn’t displaying all of the data because the loop was ending early. The progress.running attribute was returning 0 at around 8 seconds (and therefore stopping the loop) because the 12 seconds that were specified had already passed (by the time data is collected and spit out, 12 seconds have passed so progress.running=0). This caused you to only see the 8 seconds of data. It is best practice to define the loop with ‘while 1:’ because that will cause the loop to continue until the specified duration of streaming. I hope this helps!
Thank you, that is helpful.
I agree that when using “while 1”, the loop is able to successfully close at the specified duration of streaming, but independent of whether the displayed message is an error or an exception, Matlab stops executing code right after the loop ends, and the rest of the code about data processing is lost.
That is the reason I found another option to avoid the error/exception - using data.time in the loop, instead of using while 1, or progress.time_remaining, or progress.complete, or progress.running.
This in principle solves all the issues, but it’s still not clear to me why data streaming using the Matlab API is much smoother at higher sampling rates (10,000 samples/s, for example) than at 100 or 1,000 samples/s. That’s not the case when using the Moku app (not that I want to have the same performance using the Matlab API, but it still does not make sense why the “stuttering” is more pronounced at lower sampling rates).
Hello,
I think the reason that the data streaming is smoother with higher sample rates is just due to the fact that the Moku is collecting data faster so it’s able to display faster. All in all I’m glad that your script is working now. If you need anything else please don’t hesitate to reach out!