Communications between two MatLabs (1) over file

1 min read

Ref to: Communications between two MatLabs (2): over socket

It’s common that two MatLab programs needs to communicate. For instance, one program is collecting the brain imaging data but not display them, and the other program is to display the data. (Another case is at Sometimes it is not practical to merge the two program together (e.g. to keep the code clean). In this case we can run two MatLabs simultaneously. One keeps saving the data to a file, and the other keep reading the file.

Here I played with such a setup, and find they communicate well with small delay (small enough for hemodynamic responses). Check out the video below:


for ii=1:100
    disp(['write ' num2str(ii)])

last_ii = 0;
        load data
        if(ii ~= last_ii)
            disp(['get data. i=' num2str(ii)])
        last_ii = ii;

Caveat: writing/reading to/from disc is slow. So if your experiment requires real time communication without any delay (say <1ms), this method may not work. Also, the amount of data to write/read each time should be very small, and the frequency of write should be small too. The file needs to locate in your local hard drive instead of a network drive.

———- Comments ———–
Paul Mazaika from Stanford:
Cool piece of code! There may be a way to do this with one umbrella Matlab program that calls both components as subroutines. The potential advantage is that one program will keep memory in cache, not at disk, which can support rapidly updating information. For high speeds, it may be better to only occasionally update the graphical display, which otherwise may be a processing bottleneck.

Aaron Piccirilli from Stanford:
There is, sort’ve! I think Xu’s little nugget is probably best choice for many applications, but if speed is an especially big concern then there are a couple of options that I’ve come across that will maintain some sort of shared memory.

Perhaps the easiest is to use sockets to communicate data, via UDP or TCP/IP, just like you use over the internet, but locally. You write some data to a socket in one program, and read it from that same socket in another program. This keeps all of your data in memory as opposed to writing it to disk, but there is definitely some overhead for housekeeping and to move the data from one program’s memory into the operating system’s memory then back into the other program’s memory. An added bonus here: you can communicate between different languages. If you have a logging function written in Python and a visualization program in MATLAB, they can pretty easily communicate with each other via sockets.

MATLAB doesn’t have explicit parallel computing built-in like many other languages, sadly, but we all have access here to the Parallel Computing Toolbox, which is another option for some more heavy-duty parallel processing where you have a problem you can easily distribute to multiple workers.

Finally, true shared memory might be more trouble than it’s worth for most applications, as you then have to deal with potential race conditions of accessing the same resource at the same time.


More on this topic: Please continue to read Communications between two MatLabs (2): over socket

第十一期 fNIRS Journal Club 通知 2020/8/29,10am

北京时间2020年8月29日周六上午10点, 深圳大学成晓君助理教授将为大家讲解她去年发表的一篇用近红外超扫描揭示人际协调的神经机制的文章。同时,她还会和大家介绍如何用Granger causality分析两个信号的因果关系。欢迎大家参加并参与讨论。 她要讲的文献如下: Cheng, Pan, Hu, Hu (2019) Coordination Elicits Synchronous Brain Activity Between Co-actors: Frequency Ratio Matters Frontiers in neuroscience 13() 1071 Abstract: People...
Xu Cui
48 sec read

第十期 fNIRS Journal Club 视频

在2020/7/25日, 北京航空航天大学的汪待发副教授讲解了他发表的一篇BCI文章。视频如下: Youtube:
Xu Cui
4 sec read

第十期 fNIRS Journal Club 通知 2020/7/25,10am

北京时间2020年7月25日周六上午10点,北京航空航天大学的汪待发副教授,博士生导师,将为大家讲解他们组去年发表的一篇脑机交互(BCI)的近红外文章。欢迎大家参加并参与讨论。 他要讲的文献如下:Y. Zheng,D. Zhang, L. Wang, Y. Wang, H. Deng, S. Zhang, D. Li, D. Wang, “Resting-State-Based Spatial Filtering for an fNIRS-Based Motor Imagery...
Xu Cui
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *