the first argument of "warning" should be a message identifier

3 views (last 30 days)
Hello, I wrote a matlab command " warning('abc'); ", but the editor detects a problem and underscores the braketed string 'abc' with red wavy line. The given information is:
The first argument of WARNING should be a message identifier. Using a message identifier allows better control over the message.
How could I get rid of this thing? Thank you!
  2 Comments
Geoff Hayes
Geoff Hayes on 1 Sep 2014
Zhou - what version of MATLAB are you using? If I copy your line
warning('abc');
into my editor, I do not observe the behaviour that you do, and have no difficulty executing this statement (I am using R2014a). What happens if you execute this statement in the Command Window? What is returned?
Zhou
Zhou on 2 Sep 2014
Edited: Zhou on 2 Sep 2014
Hello Geoff, I am using R2012a. I didn't write the code in command line but in the '.m' file using matlab editor. The red wavy line comes out and the information prompts when the cursor is moved on it.
Still the question: what is the correct way to use 'warning' in an m-file?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 1 Sep 2014
Edited: Stephen23 on 26 Apr 2015
Either:
  • Add a message identifier. As the warning says, these are quite useful later...
Or
  • Right click where the red wavy line is, and select one of the 'Suppress...' options.
  2 Comments
Zhou
Zhou on 2 Sep 2014
Edited: Zhou on 2 Sep 2014
Thank you. Could you give me an example of a message identifier? I don't quite understand it.
Stephen23
Stephen23 on 2 Sep 2014
You can think of the basic Message as being a string intended to be displayed in the command window for the end-user to read, while the MessageID is a more formal description to help larger programs keep track of errors (e.g. automatically recover from particular errors, turn off warnings in a particular situation, query warning states, etc). Using a MessageID is good practice, it helps integrate a function into a bigger program.
You can find more info here:

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 1 Sep 2014
Did you read the help for the warning function? Actually, your code worked for me when I ran it on the command line.
I hope you didn't actually write your own function called warning and are trying to use that. Maybe you might want to do this:
warningMessage = sprintf('Hey!!!\nYou cannot do that!');
uiwait(warndlg(warningMessage));
  1 Comment
Zhou
Zhou on 2 Sep 2014
Thank you for your answer, your code works well but I want the correct way or an example to use "warning" in an m-file.

Sign in to comment.


Adam
Adam on 1 Sep 2014
In Preferences -> Code Analyzer, if you can be bothered to wait while it loads all the options the 3rd tree heading is "Errors, Warnings, etc.". In there is a checkbox for this particular warning. In mine it is switched off which I assume is the default state because I haven't explicitly changed it.
I do tend to always use that first argument now though (for errors, I rarely if ever throw a warning in my code) so maybe I should switch it on while you switch yours off!
  3 Comments
Stephen23
Stephen23 on 2 Sep 2014
While it is great that MATLAB offers the option to change the displayed warning messages, keep in mind that when distributing code you cannot rely on other users having the same options selected... unless you have a good reason, stick to the defaults.
Guillaume
Guillaume on 2 Sep 2014
You can always suppress this particular warning in your whole file with
%#ok<*WNTAG>
or on a particular line with
%#ok<WNTAG>

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!