Basic matlab q on writing error messages

  • Context: MATLAB 
  • Thread starter Thread starter brandy
  • Start date Start date
  • Tags Tags
    Error Matlab Writing
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
brandy
Messages
156
Reaction score
0
How do you write error messages
for when a function has been given an input that is illogical for what the function does.
in matlab

function b = bla(a)
for example if a < 0
i want it to display a message
 
Physics news on Phys.org
You could do something as basic as printing a string when the error occurs, e.g.:
Code:
function [output] = myfun(input)
    % do stuff
    if someCondition < minValue % define error condition however you need it
         fprintf('Error: invalid input. Input must be between foo and bar!\n');
         return;
    end
end

A better way would be to http://www.mathworks.com/help/techdoc/matlab_prog/bq_jgj8-1.html" . This way, any code that calls the function can handle errors.

Code:
function [output] = myfun(input)
    % do stuff
    if someCondition < minValue % define error condition however you need it
        err = MException('Myfun: Input Error', ...
                         'Myfun received bad input arguments.');
        throw(err);
    end
end
 
Last edited by a moderator: