1

I found this code to invoke the Matlab compiler, it works fine when the function is called from Matlab command prompt, I build this function to .Net Assembly but whenever I try to use it in my C# app in order to build some .m file I get an exception, where do you think my problem is?

Matlab Code:

function compileCode(mfile,dllName , dnetdir)

    %% Create directories if needed
    if (exist(dnetdir, 'dir') ~= 7)
        mkdir(dnetdir);
    end

    %% Build .NET Assembly
    eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:' dllName ',' ...
          '' dllName ',0.0,private'' -T link:lib ''' mfile '''']);
    end

C# code:

var cmm = new compiler.MatlabCompiler();
MWCharArray x = new MWCharArray(@"C:\Users\ePezhman\Documents\MATLAB\Graph2D.m");
MWCharArray y = new MWCharArray("Graph");
MWCharArray z = new MWCharArray(@"C:\Matlab\dotnet");
cmm.compileCode(x,y,z);

Exception:

... MWMCR::EvaluateFunction error ... Undefined function 'mcc' for input arguments of type 'char'. Error in => compileCode.m at line 9.

... Matlab M-code Stack Trace ... at file C:\Users\ePezhman\AppData\Local\Temp\ePezhman\mcrCache8.0\compil0\compiler\compileCode.m, name compileCode, line 9.

1
  • It would be much easier to invoke the command directly from C#. Using Process.Start(). Commented Mar 30, 2013 at 12:41

1 Answer 1

1

Interesting, I assume you are trying to compile a function that can dynamically compile other functions..

Unfortunately, I dont think the mcc function can be compiled/deployed itself


To be exact, the problem you are seeing is because MATLAB needs to know all functions called at compile-time, and by using eval, it wont figure it out on its own (since it wont parse inside the string). You can fix this particular issue by writing special comments for the compiler..

function myEval()
    %#function foo
    eval('...');
end

(Another alternative is using function handles).

Still even if you do that, it will fail at runtime inside the mcc function saying that: "License checkout failed, [...] Cannot find a valid license for Compiler".

The reason is as mentioned in the comments, mcc is a development tool and cannot be deployed to standalone programs which only depends on the free MCR runtime.

Think about it, if it was possible, it would defeat the whole purpose of buying licenses for the product, as you could create a standalone program that can compiler other codes without having the Compiler toolbox :)

Sign up to request clarification or add additional context in comments.

9 Comments

There's no mention of deployment. The question is purely technical. This is a comment rather than an answer.
@DavidHefernan: MATLAB Compiler (Builder NE) is a product used to generate .NET assemblies from MATLAB code, which can then be used from C#. mcc is the command used to compile the code. So it is pertaining to deployment of MATLAB code (which is an interpreted language by the way)
Yes, I know. The question is why the code fails. Not whether or not mcc can be redistributed.
because mcc function cannot be compiled itself. There is also a list of other functions that cannot be compiled.
OK, I think I understand. I think your answer is not terribly clear. However, the answer is as I said in my comment. Call mcc from C# directly. I think you could usefully add that to the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.