tags:

views:

294

answers:

3

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?

A: 

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 ''

Amro
+7  A: 

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 <=.

gnovice
How about including the actual runtime values, too? Very useful for debugging, especially once your program gets bigger. error('The crank''s length (%f) cannot exceed that of the slider (%f)', r2, r3)
Andrew Janke
A: 

You can print to the error handle as well:

fprintf(2,'The crank''s length cannot exceed that of the slider');
Zaid
Wrong language. The MATLAB function print is for printing a figure window containing graphics to a printer, and \ doesn't escape '.
Steve Eddins
@Steve: you're right. It's `fprintf`, not `print`
Zaid

related questions