Basic matlab q on writing error messages

  • Context: MATLAB 
  • Thread starter Thread starter brandy
  • Start date Start date
  • Tags Tags
    Error Matlab Writing
Click For Summary
SUMMARY

This discussion focuses on writing effective error messages in MATLAB when a function receives illogical inputs. Users are encouraged to utilize the MException class for structured error handling, allowing calling code to manage errors gracefully. An example provided demonstrates how to implement this with a custom error message using the MException class. The discussion emphasizes the importance of clear communication in error messages to enhance user experience.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with function definitions in MATLAB
  • Knowledge of error handling concepts in programming
  • Understanding of the MException class in MATLAB
NEXT STEPS
  • Research MATLAB error handling using the MException class
  • Learn about custom error messages in MATLAB functions
  • Explore best practices for user input validation in MATLAB
  • Investigate the use of try-catch blocks for error management in MATLAB
USEFUL FOR

MATLAB developers, software engineers, and anyone involved in creating robust functions that require effective error handling and user input validation.

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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K