在MATLAB和C ++之间交换数据的最有效方法? [英] Most efficient way to exchange data between MATLAB and C++?
问题描述
我正在开发一个应用程序,其中有两个程序同时运行:一个在C ++中,另一个在MATLAB中.
I'm developing an application where there are two programs, running at the same time: one in C++ and another in MATLAB.
C ++程序会定期生成三个数值作为输出. MATLAB程序需要在不同的时间段内定期访问这三个输出.
The C++ program generates periodically three numerical values as outputs. The MATLAB program needs an access to the three outputs periodically, but in a different period of time.
当前,我已使用 .txt
文件解决了此问题.例程完成后,C ++会写入此文件,而MATLAB会在需要时从文件中读取.
Currently, I've solved this using a .txt
file. The C++ writes this file when the routine is done, and the MATLAB reads from it whence it needs.
但是,使用 textscan
命令,MATLAB用奔腾I5-4250U花费大约1.5毫秒来读取.txt
.因此,我想知道是否有更快的方法来解决此问题.
However, using textscan
command, MATLAB takes around 1.5 ms to read the .txt
using a Pentium I5-4250U. So, I was wondering if there is a faster way to solve this issue.
推荐答案
A:是的,使用消息传递层
如果不需要其他服务,则C ++一方将充当信息提供者( ZMQ.PUB
发布者),而MATLAB一方将充当信息订阅者( ZMQ.SUB
strong>).
A: Yes, use a messaging layer
If no other services are needed, the C++ side will act as an information provider ( ZMQ.PUB
publisher ) and MATLAB side will act as an information subscriber ( ZMQ.SUB
).
通过这种方式,与消息传递有关的底层细节可以由分布式处理消息传递层解决,您的解决方案将受益于速度快,现成的工具,并且可以分发到私有网格计算/云计算中架构,使用相同的工具,获得更高的性能等.
This way the low-level details related to the messaging are solved by the distributed-processing messaging layer and your solution will benefit from both speed, ready-made tools and can go distributed onto a private grid-computing / cloud-computing architectures, using the same instrumentation, gaining additional performance etc.
ZeroMQ具有C ++和MATLAB的绑定,因此这里是开始和品尝过程到过程消息传递层方法的地方.
ZeroMQ has bindings for both C++ and MATLAB, so that is a place to start and taste the process-to-process messaging-layer approach.
% MATLAB script to setup zeromq-matlab
clear all;
if ~ispc
s1 = zmq( 'subscribe', 'ipc', 'MATLAB' ); %% using IPC transport on <localhost>
else
disp('0MQ IPC not supported on windows. Setup TCP transport class instead')
disp('Setting up TCP')
s1 = zmq( 'subscribe', 'tcp', 'localhost', 5555 );
end
recv_data1 = []; %% setup RECV buffer
可以在此处找到MATLAB绑定.
The MATLAB bindings can be found here.
由于要发送一些浮点数,因此延迟将在数百个以下,甚至不是几十个[usec],正如您所注意到的那样,它提到了数据分发的异步模式,因此localhost将仅花费一些时钟来从localhost ZMQ检索数据.SUB队列.
Having a few floats to send, the latencies will be somewhere under hundreds if not tens of [usec], as your notice mentioned asynchronous mode of data-dispatch, so the localhost will just spend some clocks on data retrieval from localhost ZMQ.SUB-queue.
更复杂的应用程序到应用程序之间的信号传输是可能的,只需从 ZeroMQ指南中获得启发即可.
More complex application-to-application signalling is possible, just get inspired by the ZeroMQ Guide
这篇关于在MATLAB和C ++之间交换数据的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!