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 通知 2021/04/25,10am

北京师范大学的朱朝喆教授将为大家讲解他们最近几年在经颅脑图谱(Transcranial brain Atlas) 方面做的工作。热烈欢迎大家参与讨论。 时间: 北京时间2021年4月25日周日上午10点地点: https://zoom.com房间号: 863 1929 4069密码: 113560 朱朝喆教授简介:北京师范大学教授、博士生导师;认知神经科学与学习国家重点实验室,脑成像与神经调控实验室负责人(PI), 主要从事 fNIRS 脑成像与 TMS 神经调控相关理论研究与新技术开发。主持国家自然科学基金委重点项目、科技部重大仪器专项课题等。在 Science Advances,Brain 等期刊发表 SCI 论文八十余篇,SCI 引用 11851...
Xu Cui
5 sec read

第十七期 fNIRS Journal Club 视频

北京时间2021年3月27日周六10点,北京师范大学的卢春明教授为大家讲解他们最近发布的一篇用超扫描研究恋爱关系的文章。视频如下: Youtube: Youku:
Xu Cui
4 sec read

第十七期 fNIRS Journal Club 通知 2021/03/27,10am

北京师范大学的卢春明教授将为大家讲解他们最近发布的一篇用超扫描研究恋爱关系的文章。热烈欢迎大家参与讨论。 时间: 北京时间2021年3月27日周六上午10点地点: https://zoom.com房间号: 870 6817 2529 密码: 433527 Long, Zheng, Zhao, Zhou, Zhai, Lu (2020) Interpersonal Neural Synchronization during Interpersonal Touch Underlies Affiliative...
Xu Cui
51 sec read

Leave a Reply

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