Calling MATLAB m-file and mex-file with same name from same directory

350 Views Asked by At

Short question:

I have two files in the same directory. The first file is a MATLAB .m-file, the other one is a MATLAB mex-file:

MyFunction.m

MyFunction.mexw64

Since both files would be called via MyFunction(1,2,3,'Test'), I currently can't call any of them. Is there a way to specify the extesion of the file I want to call? Maybe something like this (which does not work):

MyFunction.m(1,2,3,'Test')

If there is no easy solution, I would be forced to move the files to different directories...

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

No, there is not. This is because of MATLAB's function precedence order, which states that any MEX file on the path will always have precedence over an M-file of the same name.

The usual way around this is to use different names for the two files, and a wrapper which contains something like this:

function varargout = MyFunction(varargin)

    if exist('MyFunction_MEX', 'file') == 3
        [varargout{1:nargout}] = MyFunction_MEX(varargin{:});

    elseif any(exist('MyFunction_M', 'file') == [2 5 6])
        [varargout{1:nargout}] = MyFunction_M(varargin{:});

    else
        error([mfilename ':function_not_found'],...
              'An M file or MEX file with matching signature could not be found.');
end