1

Edit 2 After recieving a response from Mathworks support I've answered the question myself. In brief, there is an options class MWComponentOptions that is passed to the exported class when instantiated. This can, among other things, specify unique print streams for error output and regular output (i.e. from disp()-liked functions). Thanks for all the responses none the less :)

====================================================================

Just a quick question - is there any way to prevent MATLAB code from outputting to the Java console with disp (and similar) functions once compiled? What is useful debugging information in MATLAB quickly becomes annoying extra text in the Java logs.

The compilation tool I'm using is MATLAB Compiler (which I think is not the same as MATLAB Builder JA, but I might be wrong). I can't find any good documentation on the mcc command so am not sure if there are any options for this.

Of course if this is impossible and a direct consequence of the compiler converting all MATLAB code to its Java equivalent then that's completely understandable.

Thanks in advance

Edit This will also be useful to handle error reporting on the Java side alone - currently all MATLAB errors are sent to the console regardless of whether they are caught or not.

Overlord_Dave
  • 894
  • 10
  • 27
  • @DennisJaheruddin could you give an example? I'm not sure how evaluating a C/C++ function in the MATLAB code will make a difference when it's compiled to Java, thought I'd like to look into it – Overlord_Dave Dec 12 '12 at 11:05
  • 1
    Sorry I meant to refer to `evalc` (not `ceval`), I got it from this topic which is a possible duplicate: http://stackoverflow.com/questions/9518146/suppress-output – Dennis Jaheruddin Dec 12 '12 at 11:49
  • Thanks, that article is quite useful and I'll probably use it to suppress the `disp` output. It doesn't solve the issue with suppressing error output however. Thanks :) – Overlord_Dave Dec 12 '12 at 12:50

3 Answers3

2

The isdeployed function returns true if run in a deployed application (with e.g. MATLAB Compiler or Builder JA) and false when running in live MATLAB.

You can surround your disp statements with an if isdeployed block.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • useful, but still doesn't solve the problem of error output. thanks :) – Overlord_Dave Dec 12 '12 at 12:49
  • For error output, you could wrap your entire application in a big `try..catch` block. Then if the application is deployed, do nothing, otherwise rethrow the error. – Sam Roberts Dec 12 '12 at 12:58
2

I heard back from a request to Mathworks support, and they provided the following solution:

When creating whatever class has been exported, you can specify an MWComponentOptions object. This is poorly documented in R2012b, but for what I wanted the following example would work:

MWComponentOptions options = new MWComponentOptions();
PrintStream o = new PrintStream(new File("MATLAB log.log"));

options.setPrintStream(o); // send all standard dips() output to a log file
// the following ignores all error output (this will be caught by Java exception handling anyway)
options.setErrorStream((java.io.PrintStream)null);

// instantiate and use the exported class
myClass obj = new myClass(options);
obj.myMatlabFunction();
// etc...

Update

In case anyone does want to suppress all output, casing null to java.io.PrintStream ended up causing a NullPointerException in deployment. A better way to suppress all output is use to create a dummy print stream, something like:

PrintStream dummy = new PrintStream(new OutputStream() {
    public void close() {}
    public void flush() {}
    public void write(byte[] b) {}
    public void write(byte[] b, int off, int len) {}
    public void write(int b) {}
} );

Then use

options.setErrorStream(dummy);

Hope this helps :)

Overlord_Dave
  • 894
  • 10
  • 27
1

Another possible hack if you have a stand-alone application and don't want to bother with classes at all:

Use evalc and deploy your func name during compile:

function my_wrap()
    evalc('my_orig_func(''input_var'')');
end

And compile like

mcc -m my_wrap my_orig_func <...>

Well, it is obviously yet another hack.

csnemes
  • 71
  • 1
  • 1