Ahmed
Ahmed

Reputation: 69

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?

Upvotes: 6

Views: 18226

Answers (3)

gnovice
gnovice

Reputation: 125854

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

Upvotes: 12

Zaid
Zaid

Reputation: 37146

You can print to the error handle as well:

fprintf(2,'The crank''s length cannot exceed that of the slider');

Upvotes: 3

Amro
Amro

Reputation: 124563

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

Upvotes: 2

Related Questions