1

I have the following matlab script, called test.m:

disp('Hello')
error('Oooops')

I run it from bash, using:

matlab -nodisplay -nodesktop -nosplash -r  "try; cd <the appropriate folder>; test ; catch; end; quit"

The output is

Hello

In other words, I do not manage to get the error text from the bash

2 Answers 2

2

You are catching the error, without doing anything with the encountered error. Instead, catch the exception and display it. In a script/function this would look like:

try
    someNonExistingFunction()
catch ME
    disp(ME)
end

Which will display the following:

ME = 
  MException with properties:

    identifier: 'MATLAB:UndefinedFunction'
       message: 'Undefined function or variable 'someNonExistingFunction'.'
         cause: {}
         stack: [0×1 struct]
    Correction: []

When calling from bash, you could do this:

matlab -nodisplay -r "try; someFunction(); catch ME; disp(ME); end; quit"

This will make sure that you do see the error message (and the line the error occured if you display ME.stack), but allows to execute the last quit statement.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using MATLAB R2019b, use the new -batch command line option:

matlab -batch "cd <the appropriate folder>; test"

or

( cd <the appropriate folder> ; matlab -batch test )

With this option it is no longer necessary to catch errors and explicitly call exit, MATLAB exits after the given statement is finished. It also avoids loading the desktop, always writing output to stdout and stderr.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.