MATLAB Basic matlab q on writing error messages

  • Thread starter brandy
  • Start date
161
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
 

jhae2.718

Gold Member
1,110
20
You could do something as basic as printing a string when the error occurs, e.g.:
Code:
[COLOR="Blue"]function[/COLOR] [output] = myfun(input)
    [COLOR="SeaGreen"]% do stuff[/COLOR]
    [COLOR="blue"]if[/COLOR] someCondition < minValue [COLOR="SeaGreen"]% define error condition however you need it[/COLOR]
         fprintf([COLOR="Indigo"]'Error: invalid input. Input must be between foo and bar!\n'[/COLOR]);
         [COLOR="blue"]return[/COLOR];
    [COLOR="blue"]end[/COLOR]
[COLOR="Blue"]end[/COLOR]
A better way would be to http://www.mathworks.com/help/techdoc/matlab_prog/bq_jgj8-1.html" [Broken]. This way, any code that calls the function can handle errors.

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

Related Threads for: Basic matlab q on writing error messages

Replies
1
Views
1K
Replies
4
Views
7K
Replies
1
Views
3K
Replies
3
Views
2K
Replies
3
Views
6K
  • Posted
Replies
3
Views
4K
  • Posted
Replies
1
Views
2K
Replies
2
Views
2K
Top