lsl_archived icon indicating copy to clipboard operation
lsl_archived copied to clipboard

Receive two streams in Matlab

Open FenjaB opened this issue 8 years ago • 6 comments

Hello, is it possible to receive two streams in Matlab at the same time in the same script? It works with LabRecorder but I need it in Matlab. I think my problem is that Matlab waits at pull_sample at one stream and doesn't look at the other one.

FenjaB avatar Sep 05 '17 13:09 FenjaB

I don't use LSL with Matlab, but receiving two streams in Matlab should be possible. To avoid the problem you're encountering, you'll have to call pull_* with a timeout. How to handle cases when Stream-1 has no data but Stream-2 does, or vice versa, will depend on your specific use case. For example, you might need to buffer up Stream-1 data until you get something from Stream-2.

cboulay avatar Sep 05 '17 14:09 cboulay

You can use callbacks. It's not so easy to do in Matlab, but it is possible. Here is a introduction:

http://undocumentedmatlab.com/blog/explicit-multi-threading-in-matlab-part4

On 9/5/2017 3:31 PM, FenjaB wrote:

Hello, is it possible to receive two streams in Matlab at the same time in the same script? It works with LabRecorder but I need it in Matlab. I think my problem is that Matlab waits at pull_sample at one stream and doesn't look at the other one.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sccn/labstreaminglayer/issues/217, or mute the thread https://github.com/notifications/unsubscribe-auth/ADch7h8EhaSe1f3NVc6iaWq8d5kpZbcNks5sfU09gaJpZM4PM89D.

dmedine avatar Sep 05 '17 15:09 dmedine

@dmedine , is there anything specific to the Matlab wrapper that prevents two outlets from being used in the same thread?

cboulay avatar Sep 05 '17 15:09 cboulay

No, not at all. I think that spinning two threads is much more reliable than trying to coherently and reliably time two outlets that receive on the same one. That would require, among other things, buffering, and (as you point out) being careful with timeouts. The callback syntax in Matlab is a little scary at first, but in the end, I think this approach is much easier to deal with. Also, it's the way LabRecorder does it.

On 9/5/2017 5:04 PM, Chadwick Boulay wrote:

@dmedine https://github.com/dmedine , is there anything specific to the Matlab wrapper that prevents two outlets from being used in the same thread?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sccn/labstreaminglayer/issues/217#issuecomment-327204434, or mute the thread https://github.com/notifications/unsubscribe-auth/ADch7gwzM0OERjc9aB8HH6TJbappmS4Xks5sfWLxgaJpZM4PM89D.

dmedine avatar Sep 06 '17 07:09 dmedine

Thanks. I ill try that later

FenjaB avatar Sep 06 '17 08:09 FenjaB

I haven't worked much with the matlab wrapper, so there may be some complications that I am not familiar with. However, generally when dealing with multiple channels I rely on the LSL library to take care of the buffering and threading issues (which it does a good job of).

Generally I run one thread in the calling application, which sets up all the inlets required. The application runs a main loop, where it attempts to pull samples from all inlets at some high frequency. The pull command timeout is zero. If the returned time stamp is not zero, data is ready and the sample is valid. If the timestamp is zero, no data is ready.

On 9/6/2017 12:37 AM, David Medine wrote:

No, not at all. I think that spinning two threads is much more reliable than trying to coherently and reliably time two outlets that receive on the same one. That would require, among other things, buffering, and (as you point out) being careful with timeouts. The callback syntax in Matlab is a little scary at first, but in the end, I think this approach is much easier to deal with. Also, it's the way LabRecorder does it.

On 9/5/2017 5:04 PM, Chadwick Boulay wrote:

@dmedine https://github.com/dmedine , is there anything specific to the Matlab wrapper that prevents two outlets from being used in the same thread?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub

https://github.com/sccn/labstreaminglayer/issues/217#issuecomment-327204434,

or mute the thread

https://github.com/notifications/unsubscribe-auth/ADch7gwzM0OERjc9aB8HH6TJbappmS4Xks5sfWLxgaJpZM4PM89D.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sccn/labstreaminglayer/issues/217#issuecomment-327401313, or mute the thread https://github.com/notifications/unsubscribe-auth/AFC33WLXzBYlaxMWAroFyaVASvckKnOUks5sfkuggaJpZM4PM89D.

mgrivich avatar Sep 06 '17 20:09 mgrivich