MATLAB Answers

0

What is the difference between MATLAB code files, pCode and MEX files?

Asked by Doug Hull on 18 Jan 2011
I hear about all of these, but do not know how they differ.

  0 Comments

Sign in to comment.

3 Answers

Answer by Doug Hull on 18 Jan 2011
 Accepted Answer

  • M-files are plain ASCII text that is interpreted at run time. Actually it is parsed once and "just-in-time" compiled, but this is transparent to the user. Use M-files for most of your MATLAB development, and for platform independence and maintainability.
  • Pcode is a preparsed and encoded version of the M-file. Since it is preparsed, it saves on the load time of the function. This is most likely not an issue except for very large M-files, since most are parsed only once anyway. Pcode also lets you hide the source code from others. Careful, there is no way to convert Pcode back to the M-file source. Pcode is platform independent.
  • MEX files are native C or C++ files that are dynamically linked directly into the MATLAB application at runtime. They must be compiled for each hardware architecture on which they are to be run. MEX files have the potential to crash the MATLAB application, but rather large speed gains are possible, depending on the algorithm.
[From the MATLABFAQ of Ancient Times]

  2 Comments

MEX files can also be Fortran source :)
Another difference is that arguments to MEX routines are passed by reference whereas arguments to m-files are passed a shared-data-copy. One exception is that cell or structure field references are resolved before the MEX routine is called, so in those cases the MEX routine gets passed a shared-data-copy of the cell or structure field. Another difference is that MEX routines can be MATLAB version dependent, so a MEX routine compiled under one MATLAB version may not run under another MATLAB version, even on the same machine.
Also, Pcode is MATLAB version dependent. Pcode generated under one MATLAB version may not run under a different MATLAB version.

Sign in to comment.


Answer by Alexei
on 12 Feb 2011

I actually want to add a question ))
I know you can turn .m code to C files using the MCC and then turn that to a MEX dll
Question is, can you turn .p code to C using MCC and then turn to a MEX dll?

  1 Comment

mcc cannot turn .m files to C using any version for a number of years now. mcc produces data files that contain threaded interpreted code, not C code. mcc has no problem including .p files in producing the final result.

Sign in to comment.


Answer by Rui
on 27 Apr 2012

is there any difference in performance on using the MCC to build a standalone application or matlab compiled dll to be called within a C/C++ application?

  1 Comment

No, other than perhaps differences in the start-up time (and those differences could depend upon whether a graphics window was being created.)
However, in the other question you were asking about this, you were talking about converting code to C or C++. Converting code to C or C++ is handled by MATLAB Coder, not MATLAB Compiler, and the result would run at the speed of C (or C++), not at the speed of MATLAB. The trade offs is that MATLAB Coder is expensive and there are quite a number of things that cannot be compiled with it.

Sign in to comment.