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?
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.
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.
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.
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 <=
.
You can print to the error handle as well:
fprintf(2,'The crank''s length cannot exceed that of the slider');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With