3

I am using the command applicationCompiler -package harmonic_viewer.prj to compile a MATLAB program for distribution. Sometimes I miss a file and the application compiler throws an error. I would like to catch such errors and pass the error up to terminate a Windows batch script that compiles a number of programs.

It seems from the documentation that applicationCompiler does not return a pass/fail result. It does throw an error that appears in red in the MATLAB command window. I believe I should be able to catch the error in a try-catch-end block, but it does not work for me. Here is an example session

>> try
applicationCompiler -package harmonic_viewer.prj
catch me
disp('ac error')
end
Error: Caught exception when packaging project: C:\build_matlab\source\harmonic_viewer\harmonic_viewer.prj
com.mathworks.project.api.InvalidProjectException
File filteredunwrap.m from Files required for your application to run does not exist.
>> 

I expected the try-catch-end block to trap the error and print 'ac error'.

Can anyone see what I am doing wrong?

I am using MATLAB r2014a on 64 bit Windows 7.

3
  • Did you verify it is really an exception? I don't find any documentation for applicationCompiler (probably you need a license to view it) but it might be that it returns an error code and does not throw exceptions. Could you run applicationCompiler -package harmonic_viewer.prj;disp('no error');? Do you see the disp?
    – Daniel
    Mar 2, 2016 at 12:25
  • For r2015b, see here. The deployTool used to come in three flavours depending what you want to build, and the applicationCompiler is the flavour the builds then packages a compile program into an installer. It really is an error; I renamed one of the source files. The applicationCompiler is a bit odd. It runs in the background, so the command prompt comes back but the compiler keeps emitting more information. When the error is raised, it is raise early like a normal MATLAB error and control returns to command prompt. Mar 2, 2016 at 13:58
  • If it runs in the background, then it must be some issue similar to this one where your try/catch is no longer part of the stack. It is a bit tricky to debug the stuff without having the code, but you could try the following. Put the code in a function called foo and run the code. After receiving the error type in rethrow(lasterror), what does it return? My guess is, foo is not part of the stack. lasterror might also be the only way to get the exception, the workaround I suggested in the linked answer probably does not apply here.
    – Daniel
    Mar 2, 2016 at 14:05

1 Answer 1

2

The compilation is run on another thread - you know this by the fact you can interact with Matlab while the compilation is going on.

The way to fix this (its a bit of a hack) is to create a post build method which will read the command window and look for key words, namely "Package failed" or "package finished" or in your case "Error: Caught"

e.g.

clc
applicationCompiler -package yourProject.prj
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
% loop until condition found
while true
  pause ( 2 )
  myTxt = cmdWinDoc.getText(cmdWinDoc.getStartPosition.getOffset,cmdWinDoc.getLength);
  %
  if ~isempty ( strfind ( myTxt, 'Package finished' ) )
    fprintf ( 'Woo hoo - it worked!!! :)\n' );
    break
  end
  if ~isempty ( strfind ( myTxt, 'Package failed' ) )
    fprintf ( 'Uh oh - it failed!!! :)\n' );
    break
  end
end

Note: You should put a time limit check in the loop as well to capture an infinite loop situation...

You can of course extend this to search for the errors and keywords for your case.

2
  • +1 for simply knowing how to do it. To simplify parsing, it might be a good idea to use the isbusy implemented here and wait until everything is finished, then get the output. This way it might also be possible to remove the clc and just look at the last message.
    – Daniel
    Mar 2, 2016 at 14:31
  • 1
    Thanks. The statusbar doesn't say busy when the compilation is running so you cant use that. The clc is not required and there is other ways around it -> but I find it the easiest and cleanest way to do it! :)
    – matlabgui
    Mar 2, 2016 at 15:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.