1

I want to deploy an m file into an executable. I am using mcc command: mcc -m epidemic.m. Epidemic is my function which takes no arguments and returns a vector and write that vector to txt. Mcc creates epidemic.exe and when I am running that exe it creates the txt file however it seems that it doesnt return values (the return value of .exe). I am trying to run the exe from matlab using:

cmd = ['epidemic.exe '];
system(cmd);

It return cmdout " and status 0. How can I take the returned values of the .exe?

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152

2 Answers2

3

An exe does not have a return value, you need to find another way to transport the data back, for example via console outputs or text files. What you get is the error code and error message.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • How can I transport our data to console output? – Jose Ramon Jan 29 '15 at 11:54
  • Moreover, is it possible to give input to my matlab function from terminal? If I have input argument to my .m file can I parse them from terminal when I call the deployed .exe? – Jose Ramon Jan 29 '15 at 12:37
  • Arguments to .exe are passed to .m file as strings. You can add conversion at the beginning of your .m file (for instance, `if ischar(param1), param1 = str2num(param1); end`) to get correct type on matlab side. – CitizenInsane Jan 29 '15 at 12:59
  • For example I got the following function I want to deploy to .exe. function final = epidemic(str1, str2, str3). If input of the m files are string it should work? – Jose Ramon Jan 29 '15 at 13:36
3

When you compile matlab code like:

function [out1, out2] = epidemic(in1, in2, in3)
%[
    ...
%]

to standalone (mcc -m epidemeic.m), Matlab produces somehow the following pseudo c-code and compiles it to .exe:

int main(int argc, char** argv)
{
     // Load compiled code produced by mcc
     HMCRInstance* hInst = loadByteCodeProducedByMccFromResources();

     // Similar to have wrote in matlab "epidemic(argv[0], argv[1], ...)"
     // 1) Without asking for any argument output
     // 2) Argument inputs are passed as strings
     int errorCode = mclFevalFromExeArg(hInst, "epidemic", argc, argv);

     return errorCode; // only indicates if call to 'mclFEvalFromExeArg'
                       // succeded, it does not relate to out1, out2 at all.
}

NB: If you want to see the exact code produced by mcc, use mcc -W main -T codegen epidemic.m

So, directly compiling to standalone, you cannot work with outputs of your Matlab function. If you need to play around with output arguments of epidemic, either

  • [Simple solution] Consider saving outputs to files or display them to shell console using disp (NB: you can use isdeployed in your .m file to check if you're running from matlab or from compiled code).

  • [Advanced solution] Consider compiling your code to shared library (mcc -l epidemic.m) instead of standalone (mcc -m epidemeic.m)

NB: When you compile your code to shared library, mcc will produce a dll that exports the following function:

extern LIB_epidemeic_C_API 
bool MW_CALL_CONV mlxEpidemic(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

nrhs/prhs are the number of input arguments and their values (as mxArray type). And nlhs/plhs are the ouput arguments you want to have when calling epidemic. Up to you to do the marshaling between mxArray and equivalent C native type.

EDIT

As you indicate that epidemic returns a vector of values, you can display them from standalone like this:

function [output] = epidemic(v1, v2, v3)
%[
    % When called from system cmd line, v1, v2, v3 are passed
    % as string. Here is how to convert them to expected type if required
    if (ischar(v1)), v1 = str2double(v1); end
    if (ischar(v2), v2 = str2double(v2); end
    if (ischar(v3)), v3 = str2double(v3); end

    ...
    output = ...;
    ...

    if (isdeployed())
        disp(output);  
    end
%] 
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
  • I have got issues creating lib, thus I decided the solution of executable. I wrote the output in text files. What should I do for the input case? Is it possible to take as argument the input of matlab function? – Jose Ramon Feb 03 '15 at 14:22
  • Again, yes ... if you write on system console `epidemic.exe riri fifi loulou` it is eaxctly the same as if you had wrote on matlab promt `epidemic('riri', 'fifi', 'loulou')` ... All arguments are passed as string, if required you can convert them at the beginning of the function using `ischar` ... see updates in my answer. – CitizenInsane Feb 03 '15 at 15:07