Search code examples
javamatlabconsolematlab-deployment

Supressing console (disp) output for compiled MATLAB > Java program (using MATLAB Compiler)


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.


Solution

  • 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 :)