How to run external .m code in a MATLAB compiled application?

2.8k views Asked by At

I've got a MATLAB project, which I compile in order to have a single executable file, using MCC.

Then I would want to know if it's possible for an external programmer to execute some of his .m files within the .exe, without re-compiling the whole project.

The point is to provide an application in which other developpers could add their "plug-ins", written in MATLAB.

I've searched a way of running external .m files inside compiled MATLAB application (like this thread : Running an .m file from a MATLAB-compiled function) but it doesn't fit my purposes here, altough it's working fine using eval().

But this eval() "trick" isn't sufficient, as it doesn't allow to define new functions or classes. For instance, I would like external .m files to be new classes (inherited from compiled "interfaces" in the executable).

Is there a way to dynamically load .m files into a MATALB compiled executable ? (even if it needs a MATLAB licence to do such).

And/or is there some "undocumented MATLAB" that refers to this particular topic that I could investigate further ?

Regards,

2

There are 2 answers

2
Sam Roberts On

If you were able to create and distribute a compiled application that could execute arbitrary .m files, your users would be able to do pretty much anything MATLAB can do, but for free (even if that wasn't your intent).

Providing them with that capability (even if you intended something more innocent and useful) is against the license agreement for MATLAB Compiler, and MathWorks also put in place some technical restrictions to make it difficult to do so.

You might find a partial way around some of the technical restrictions, but if you give your users the ability to execute arbitrary m-code in a plugin, you'll be in breach of the license.

(Of course IANAL)

2
Oli On

I think that the only way is to do some system calls from your compiled function, like:

mFile2Launch='foo'; %%% or whatever input
system(['matlab -r "' mFile2Launch '"']);

or you can also use that more complicated line to make sure everything work well:

system(['matlab -nodesktop -nosplash -nodisplay -r "try, ' mFile2Launch '; end; quit"'])