"warning takes sprintf-like arguments directly" not entirely true?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
If I write this:
message = sprintf('%d %d %d', [1 2 3])
warning(message)
I get a warning from MATLAB Code Analyzer at the second line, saying "warning takes sprintf-like arguments directly".
However if I follow its recommendation and I type:
warning('%d %d %d', [1 2 3])
I get the error "Formatted arguments cannot be non-scalar numeric matrices.".
Is this the desired behaviour?
4 comentarios
Massimiliano Zanoli
el 19 de Sept. de 2018
Editada: Massimiliano Zanoli
el 19 de Sept. de 2018
Respuestas (2)
Image Analyst
el 19 de Sept. de 2018
Get rid of the brackets so you have scalars instead of a vector:
>> warning('%d %d %d', [1 2 3])
Error using warning
Formatted arguments must be scalar.
>> warning('%d %d %d', 1, 2, 3)
Warning: 1 2 3
So it must not like that fact that you're trying to put in one matrix with 3 format specifiers (%d).
2 comentarios
Jan
el 19 de Sept. de 2018
Editada: Jan
el 19 de Sept. de 2018
This is the documented behavior [EDITED] since R2018b:
doc warning
You find:
warning(msg,A1,...,An)
A1,...,An — Values
character vector | string scalar | numeric scalar
Values that replace the conversion specifiers in msg, specified as a
character vector, string scalar, or numeric scalar.
[1,2,3] is not a numeric scalar.
So either use sprintf or provide the elements separately:
data = [1,2,3];
dataC = num2cell(data);
warning('%d %d %d', dataC{:})
While it is documented, one could still discuss, if this is the "desired" behavior. I did not struggle with this feature in the last 20 years.
8 comentarios
Matthias Roosz
el 28 de En. de 2022
An old topic - but still annoying for those not switching to new matlab releases every year...
IMHO totally undesired behaviour. I liked using sprintf-like arguments to warning (and error) - as the message hint STILL suggests. All code relying on dealing array values to multiple format specifiers breaks with the change to R2018b or later with no hint to be found in the release notes or the code analyzer :'(
distrib = [0.23 3.1415 123.4]; % dummy values
% warning('bad distrib (1%%/median/99%%): %.1f/%.2f/%.1f', distrib) % used to work
warning('bad distrib (1%%/median/99%%): %.1f/%.2f/%.1f', distrib(1), distrib(2), distrib(3)) % how is this better?
distribC = num2cell(distrib);
warning('bad distrib (1%%/median/99%%): %.1f/%.2f/%.1f', distribC{:}) % or this?
A solution (apart from repeating the variable name holding the data to be printed) that does not require an extra line of code before each warning/error would still be interesting. Maybe we'll get something like python's *args? I've tried this
expand = @(x) x{:};
warning('bad distrib (1%%/median/99%%): %.1f/%.2f/%.1f', expand(num2cell(distrib)))
but no luck because the function only 'sees' one argout, thus only reports the first value...
Walter Roberson
el 29 de En. de 2022
No mlint warning for this, at least not in current releases:
distrib = [0.23 3.1415 123.4]; % dummy values
SIT = @(x) struct('d', num2cell(x));
warning('bad distrib (1%%/median/99%%): %.1f/%.2f/%.1f', SIT(distrib).d)
This relies upon a somewhat new feature, that the return value of a function call may now be indexed with dot notation. Unfortunately I am having difficulty finding the appropriate release notes entry to say which release this started with.
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!