Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display an error message in MATLAB?

Tags:

matlab

I was doing a model for a slider-crank mechanism and I wanted to display an error for when the crank's length exceeds that of the slider arm. With the crank's length as r2 and the slider's as r3, my code went like this:

if r3=<r2
    error('The crank's length cannot exceed that of the slider')
end

I get the error:

???     error('The crank's length cannot exceed that of the slider')
                         |
Error: Unexpected MATLAB expression.

can someone tell me what I'm doing wrong and how to fix it please?

like image 661
Ahmed Avatar asked Dec 13 '09 19:12

Ahmed


People also ask

Which function is used to display an appropriate message when error occurs?

The C programming language provides perror() and strerror() functions which can be used to display the text message associated with errno. The perror() function displays the string you pass to it, followed by a colon, a space, and then the textual representation of the current errno value.

What are the three types of errors in MATLAB?

There are several different kinds of errors that can occur in a program, which fall into the categories of syntax errors, runtime errors, and logical errors. Syntax errors are mistakes in using the language.

How do I raise an exception in MATLAB?

Use the throw or throwAsCaller function to have MATLAB® issue the exception. At this point, MATLAB stores call stack information in the stack field of the MException , exits the currently running function, and returns control to either the keyboard or an enclosing catch block in a calling function.


2 Answers

When you want to use the ' character in a string, you have to precede it with another ' (note the example in the documentation):

if (r3 <= r2)
  error('The crank''s length cannot exceed that of the slider');
end

Also, note the change I made from =< to <=.

like image 76
gnovice Avatar answered Oct 09 '22 10:10

gnovice


You can print to the error handle as well:

fprintf(2,'The crank''s length cannot exceed that of the slider');
like image 36
Zaid Avatar answered Oct 09 '22 10:10

Zaid