MATLAB: How to build .m files into .mex files correctly

build matlab source file (.m) into mex fileMATLAB Compiler

Hi All,
I am trying to build all my M files into MEX files so that I can give them to other users to use without disclosing the source code. I used the following steps to create (suppose I have a function blank saved in blank.m, code pasted down at the bottom):
Platform: Windows 7, with Matlab 2010b, visual studio 2010
1. convert M into C++ header and Library by:
mcc -W cpplib:blank -T link:lib blank.m
2. Use the generated blank.h and blank.lib, I create a blankm.cpp which looks like
void mexFunction(int nlhs,
mxArray* plhs[],
int nrhs,
mxArray* prhs[])
{
// validate input argument number
if (nrhs < 1)
{
mexErrMsgTxt("At least the file name is required.");
}
else if (nrhs > 1)
{
mexErrMsgTxt("Too many input arguments.");
}
// validate output argument number
if (nlhs > 1)
{
mexErrMsgTxt("Too many output arguments.");
}
mlxBlank(nlhs,
plhs,
nrhs,
prhs);
}
3. Then, I compiled it using matlab:
mex blankm.cpp -L blank.lib
Or compile it in Visual Studio 2010. All of the above steps went well with no problem. So far I have generated blankm.mexw64 successfully. If I input the following script in Matlab, it comes out with nothing
blandm(' a ')
However, if I input like
a = blankm(' a ');
I'll get an error
??? One or more output arguments not assigned during call to "blankm".
Any idea on this? I did something wrong. Or, the whole path is not correct? Thank you in advance. function str2 = blank(str)
if ischar(str)
str2 = fliplr(deblank(fliplr(deblank(str))));
else
error(['input is not a string']);
end
end

Best Answer

Shared libraries generating using MATLAB Compiler are meant for deploying on a machine that does not have MATLAB installed - this is not needed in your case since you only want code-protection.
Why not simply pcode your MATLAB-files? If you absolutely want to create a MEX-file however, the better option might be to use MATLAB Coder which will generate standalone C code from your MATLAB functions where possible. If some of your MATLAB code is not supported for code generation, they will be converted into mexCallMATLAB calls.