Effacer les filtres
Effacer les filtres

How to get warning source

74 vues (au cours des 30 derniers jours)
L. F.
L. F. le 14 Juil 2024 à 20:35
Commenté : dpb le 16 Juil 2024 à 18:02
Good day everyone,
I have a very long matlab code, and I get the folowing warning when runing (about as much as matlab can spill out into the console per second):
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
How do I get the file / line number generating this warning?
Thank you very much.

Réponse acceptée

dpb
dpb le 14 Juil 2024 à 21:20
>> x(i:i)
Warning: Colon operands must be real scalars.
Array indices must be positive integers or logical values.
>> [w,wid]=lastwarn
w =
'Colon operands must be real scalars.'
wid =
'MATLAB:colon:operandsNotRealScalar'
>>
The above generated the warning but also errors, but if you've managed to only generate the warning but not an error, then
dbstop if warning
will stop at the first warning; enter the text of the warning ID (wid return variable from lastwarn) to be the specific warning.
dbstop if warning MATLAB:colon:operandsNotRealScalar
  9 commentaires
Steven Lord
Steven Lord le 16 Juil 2024 à 15:42
The above appears to me to have simply been an oversight or maybe somebody over the years introduced size in place of an initial use of the risky length without including the dim argument. Since it only generated a warning and didn't crash and burn,
FYI, that is planned to change, as the warning message the original poster quoted states. From the Release Notes, "colon now issues a warning when creating vectors if one or more operands are not scalar. For example, expressions like [1 2 3]:2:10 now warn. This warning will become an error in a future release, removing support for nonscalar operands. Previously, colon used the first element of any nonscalar operands to evaluate the expression." [Emphasis added.]
1:size(something) is a common pattern that probably doesn't do what the author intended it to do. Usually either adding the dimension input (if you want to iterate over the rows / columns / pages / etc. of something) or replacing size with numel (if you want to iterate over all the elements using linear indexing) is probably the solution.
dpb
dpb le 16 Juil 2024 à 18:02
Thanks for the reminder of colon behavior with nonscalar elements; I knew that but forgot about it in concentrating on the for construct and thinking about how prevelant the use of length was; it seems to be somewhat less so recently although it is still quite dangerous in that it also may not do what the user expects.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 15 Juil 2024 à 1:20
Learn to debug your code. Just step through it one line at a time until you see the error appear. See this link
so you can do it yourself and not have to wait for us to figure it out for you.
  1 commentaire
L. F.
L. F. le 15 Juil 2024 à 9:06
Modifié(e) : L. F. le 15 Juil 2024 à 10:49
Thank you, but I cannot debug it step by step, more than 30k-50k of lines, after 2 hours didn't reached any lines that throws that warning. Is there a way to get Matlab to tell the line number where iit throws that warning?

Connectez-vous pour commenter.

Tags

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by