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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With