the first argument of "warning" should be a message identifier
3 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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
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:
More Answers (2)
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));
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
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
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>
See Also
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!