Putting ‘error’ And ‘warning’ Signs In MatLab Codes

In Matlab® programming, you can put error or warning exclamations inside codes to redirect the code users. Here, we explain the differences between error and warning exclamations in Matlab® and how to use them with very basic examples below.

YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn MatLab®!

How To Use ‘warning’ And ‘error’ Commands In MatLab®?

Take a look at the very basic example below to understand the use of ‘error’ in Matlab®.

>> x = input('enter a negative number');
y = input('enter a negative number');
if x >=0
error('you needed to enter a negative value for x.')
elseif y >=0
error('you needed to enter a negative value for y.')
end

x+y
enter a negative number6
enter a negative number6
you needed to enter a negative value for x.

>>

As you see that we taking the values of ‘x’ and ‘y’ from the user with the ‘input()’ command. And the values of ‘x’ and ‘y’ must be negative. And we placed an if query that if the entered number is bigger than zero, it gives the exclamation of ‘you needed to enter a negative value for x/y.’.

We did this by using the ‘error()’ command in Matlab®. After the exclamation in the command window as you see above, the error() command halts all programs. And the required operation is not done because the error() command is used.

Take a look at the example below to understand the use of ‘warning()’ command in MatLab®.

>> x = input('enter a negative number');
y = input('enter a negative number');
if x >=0
warning('you needed to enter a negative value for x.')
elseif y >=0
warning('you needed to enter a negative value for y.')
end

x+y
enter a negative number6
enter a negative number5
Warning: you needed to enter a negative value for x.

ans =

11

As you see above example in Matlab®, we are taking the values of ‘x’ and ‘y’ from the user with input() command, and we want that these values must be negative. We again used the if-else query if the user enters any positive value, it gives a warning in the command window as you see above. But the operation is also done.

So, the warning() command does not halt the program, the program will work if the warning() appears in the command window.

You can understand the simple difference between the uses of warning() and error() commands in Matlab®. And because of the better user experience, the use of these commands can be better.

Conclusion

If you want further examples about the use of warning() and error() commands in MatLab®, please state them in the comments.

Do not forget to leave your comments and questions about the use of error() and warning() commands in Matlab® below.

Your precious feedbacks are very important for us.