How can I display an error message in 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?
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');
I believe the comparison operator should be <=
not the other way around, unless that was only a typo in your question
Also you should escape the '
character using ''
精彩评论