Matlab 如何知道有一个 .mex64 文件并避免无穷大"compiling"循环



我创建了mymex.m和mymex.cpp。在.m内部,我使用MEX编译.CPP。仅当.mex64不存在时,才会发生。.mex64符合MATLAB路径中的目录。但是,如果我不将MATLAB电流设置为.mex64 dir,则MATLAB将继续在Infinity循环上运行.m。我缺少什么?

mymex.m:

function [dataOut] = MyMex(dataIn)
mexCppFile = 'MyMex.cpp';
mexCmd = 'mex MyMex.cpp;';
fprintf('nFile %s not compiled yet, compiling it now...n%sn',mexCppFile,mexCmd);
fileFullPath = which(mexCppFile);
if size(fileFullPath,2) > 0 && exist(fileFullPath,'file')
    [fileDir, fileName, ext] = fileparts(fileFullPath);
    curDir = pwd;
    cd(fileDir);
    mex MyMex.cpp;
    cd(curDir);
else
    error('prog:input','Unable to find %s to compile it. Check if the file is in the current dir or in the Matlab PATH!',mexCppFile);
end
% Call C++ mex
[dataOut] = MyMex(dataIn)
end

edit 捍卫自己免受我无限循环的评论:MATLAB应该知道该功能有一个编译版本。我不知道它是如何做到的,我的问题与此相关,因为有时它在某些时候发现了功能。

这是一个合并的在线MEX样本,可以做相同的"无限"事物并顺利进行:

2D插值

Mirt2d_mexinterp.m中的代码:

% The function below compiles the mirt2D_mexinterp.cpp file if you haven't done it yet.
% It will be executed only once at the very first run.
function Output_images = mirt2D_mexinterp(Input_images, XI,YI)
pathtofile=which('mirt2D_mexinterp.cpp');
pathstr = fileparts(pathtofile);
mex(pathtofile,'-outdir',pathstr);
Output_images = mirt2D_mexinterp(Input_images, XI,YI);
end

也许.m和.mex64需要在同一文件夹上。

这一切都归结为Matlab的搜索路径。如果Mex文件在路径中相同的水平,则优先考虑它们。当前目录中的文件优先于MATLAB搜索路径中其他位置的文件。因此,当您遇到无限循环时,很明显,M文件在搜索路径中比MEX文件更高。

本质上,如果两个文件在同一文件夹中,一切都很好。

最新更新