简体   繁体   English

如何运行从Matlab工作区传递变量的.exe程序?

[英]How to run .exe programs passing variables from Matlab workspace?

I 'm working on a Matlab project that requires large data processing and some parts of the code need to run faster than Matlab functions. 我正在一个需要大量数据处理的Matlab项目中,并且某些代码部分需要比Matlab函数运行得更快。 For this purpose I need to call an .exe inside other scripts passing variables from the workspace. 为此,我需要在其他脚本中调用.exe,以从工作区传递变量。 In order to understand how I could solve that a created a small addition program. 为了了解如何解决这个问题,我创建了一个小的加法程序。

I have the following code 我有以下代码

function test(a,b)
if ischar(a)
  a2=str2num(a);
else
  a2=a;
end
if ischar(b)
  b2=str2num(b);
else
  b2=b;
end
res=a2+b2;
disp(res)

and I used the deployment tool in order to make it executable. 为了使它可执行,我使用了部署工具。 If I run the test.exe through matlab with !test.exe 5 3 it works, If I create two variables a=5 and b=3 and try !test.exe ab it doesn't work. 如果我使用!test.exe 5 3通过matlab运行test.exe,它将起作用;如果我创建两个变量a = 5和b = 3并尝试!test.exe ab,它将无法正常工作。

I know that I can pass the variables to a .txt or .dat file and then close and re opened again through the program (the variables that I need to use are dynamic) but I don't believe that its more efficient than running mfile loading variables from workspace. 我知道我可以将变量传递到.txt或.dat文件,然后通过程序关闭并重新打开(我需要使用的变量是动态的),但我不认为它比运行mfile更有效从工作区加载变量。

I also searched about the use varargin,nargin etc. but these commands don't have the use of argc[], argv[] of C. Something like that it could solve my issues. 我还搜索了有关使用varargin,nargin等的信息,但是这些命令没有使用C的argc [],argv []。类似的东西可以解决我的问题。

Then I search for mex files and write the following code: 然后,我搜索mex文件并编写以下代码:

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
   mxArray *mexGetVariable(const char *workspace, const char *varname);

   const mxArray *mex_a;
   const mxArray *mex_b;

   //http://www.mathworks.com/help/techdoc/apiref/mexgetvariable.html
   if ((mex_a = mexGetVariable("a", "global"))==NULL)
   {
        mexErrMsgTxt("Variable 'a' not in workspace.");
   }
   else if ((mex_b = mexGetVariable("b", "global"))==NULL)
   {
        mexErrMsgTxt("Variable 'b' not in workspace.");
   }
   else
   {
        mexEvalString("!test.exe mex_a mex_b"); 
   }
} 

(I have also passed variable a=5 b=3) But nothing worked as I have a prompt saying Variable a not in the workspace. (我还传递了变量a = 5 b = 3)但是没有任何作用,因为我提示说变量a不在工作区中。

Can anyone provide me a code solution on how can I make .exe programs reading variables from matlab workspace without opening .txt or .dat files? 谁能为我提供一个代码解决方案,说明如何使.exe程序能够在不打开.txt或.dat文件的情况下从matlab工作区读取变量?

Thank you in advance for your kindness in reading my topic. 预先感谢您阅读我的话题的好意。

The command !test.exe ab is trying to run test.exe on the strings 'a' and 'b' , not the values of a and b . 命令!test.exe ab试图在字符串'a''b'而不是ab的值上运行test.exe This would be the case whether you ran it from the command line or via a mex file. 无论是从命令行运行还是通过mex文件运行,都会出现这种情况。

If you do something like: 如果您执行以下操作:

>> a=5; b=3;
>> cmdstr = sprintf('!test.exe %f %f',a,b)
cmdstr =
!test.exe 5.000000 3.000000
>> eval(cmdstr)

That would call it in the way I think you're intending. 那将以我认为您打算的方式来称呼它。

Is your real .exe (not the test.exe) created from MATLAB with MATLAB Compiler? 您使用MATLAB Compiler从MATLAB创建的真实.exe(不是test.exe)吗? If so, the above still may not achieve what you're looking for. 如果是这样,上述可能仍然无法实现您想要的目标。 Executables created using MATLAB Compiler run at the same speed as live MATLAB. 使用MATLAB Compiler创建的可执行文件的运行速度与实时MATLAB相同。

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

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