简体   繁体   English

具有C可执行文件的Matlab系统功能

[英]Matlab system function with a C executable

I have written a Matlab GUI for my C program. 我已经为我的C程序编写了Matlab GUI。 I thought about using MEX, but there are too many C files and C program requires a DLL to run. 我考虑过使用MEX,但是C文件太多,C程序需要DLL才能运行。

So, instead I have the Matlab System function calling the executable with inputs, something like [status results] = system('executable "input 1" "input 2"') , which runs well, but I want real time output. 因此,取而代之的是,我让Matlab系统函数使用输入来调用可执行文件,例如[status results] = system('executable "input 1" "input 2"') ,它运行良好,但我想实时输出。 results is just a percent output of how complete the program is, and I want to use this output for a GUI progress bar in Matlab. results只是程序完成程度的百分比输出,我想将此输出用于Matlab中的GUI进度条。

The output does get stored into results , but only after the program is complete. 输出确实会存储到results ,但仅在程序完成之后。 Thus, making the progress bar pointless. 因此,使进度条毫无意义。

Is it possible to get the executable to send outputs one at a time to Matlab, and then have Matlab update the progress bar, and return to the executable? 是否有可能使可执行文件一次将输出发送到Matlab,然后让Matlab更新进度栏,然后返回可执行文件?

Edit: I'm looking for a solution in Windows. 编辑:我正在寻找Windows中的解决方案。

I only see two options, and neither fits directly with your current implementation approach. 我只看到两个选项,两个都不能直接适合您当前的实现方法。

The first, is to just use sockets to communicate between the two. 首先,就是使用套接字在两者之间进行通信。 Here's a pure matlab socket implementation, but under the hood it's using C sockets. 这是一个纯matlab套接字实现,但实际上它是使用C套接字。 It's been 10 years since I've done C/Java socket comms, but I recall that at the time there were some issues. 自从完成C / Java套接字通信已经10年了,但是我记得当时存在一些问题。

http://www.mathworks.com/matlabcentral/fileexchange/21131-tcpip-socket-communications-in-matlab http://www.mathworks.com/matlabcentral/fileexchange/21131-tcpip-socket-communications-in-matlab

Another option is to have your executable be accessible via a C DLL from matlab, and call the DLL directly from matlab (ie have matlab control your app). 另一个选择是使可执行文件可通过matlab的C DLL进行访问,并直接从matlab调用DLL(即让matlab控制您的应用)。 This is the way I've been doing most such interactions lately, and it works very well. 这是我最近进行大多数此类交互的方式,并且效果很好。

http://www.mathworks.com/help/techdoc/ref/loadlibrary.html http://www.mathworks.com/help/techdoc/ref/loadlibrary.html

I found a solution. 我找到了解决方案。 Credit goes to Richard Alcock at Matlab Central 归功于Matlab Central的Richard Alcock

Specifically, for my solution: 具体来说,对于我的解决方案:

cmd = {'executable.exe', 'input 1', 'input 2'};
processBuilder = java.lang.ProcessBuilder(cmd);
cmdProcess = processBuilder.start();

% Set up a reader to read the output from the command prompt
reader = 
    java.io.BufferedReader(...
        java.io.InputStreamReader(...
            cmdProcess.getInputStream() ...
        ) ...
    );

% Loop until there is some output
nextLine = char( reader.readLine );
while isempty(nextLine) 
    nextLine = char( reader.readLine );
end

% Then loop until there is no more output
while ~isempty(nextLine);
    fprintf('Output: %s\n', nextLine);
    nextLine = char( reader.readLine );
end

% Get the exit value of the process
exitValue = cmdProcess.exitValue  

Note: this code does not hold up the executable. 注意:此代码不包含可执行文件。 The executable must finish before this code finishes, otherwise this code crashes when it gets ahead of the executable. 可执行文件必须在此代码完成之前完成,否则,当该代码位于可执行文件之前时,该代码将崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM