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.

 Akzeptierte Antwort

dpb
dpb am 14 Jul. 2024

1 Stimme

>> 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 Kommentare

L. F.
L. F. am 15 Jul. 2024
Thank you very much,
dbstop if warning MATLAB:colon:operandsNotRealScalar
this works!
dpb
dpb am 15 Jul. 2024
So, what was the actual error (curious minds and all...)?
L. F.
L. F. am 16 Jul. 2024
Bearbeitet: L. F. am 16 Jul. 2024
It was a
for i = 1:size(somevector)
the solution is
for i = 1:size(somevector, 1)
dpb
dpb am 16 Jul. 2024
Ah, so...a fairly common oversight/mistake/typo...
L. F.
L. F. am 16 Jul. 2024
Yes, the code in question is about 10 years old, and overall the project is about 30-50k lines. This occured about 6 times. Without the dbstop I would have never found it. Thank you again.
Image Analyst
Image Analyst am 16 Jul. 2024
Evidently it wasn't a vector like the author thought, so the size() function is returning 2 or more numbers instead of a single number. While your modification fixes the for statement, you might try to track down the root cause of why somevector is not really a vector as expected.
dpb
dpb am 16 Jul. 2024
Bearbeitet: dpb am 16 Jul. 2024
"...it wasn't a vector like the author thought, so the size() function is returning 2 or more numbers instead of a single number."
size(x) always returns at least two values unless it is called with the optional dim argument to limit it to one or more dimension. All native types in MATLAB are arrays of at least two dimensions; even an [] empty array.
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, may have just lived with it because like @L. F., it was buried in such a mass of code they couldn't find where it was coming from.
size([])
ans = 1x2
0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(1)
ans = 1x2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v=1:3; size(v)
ans = 1x2
1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I'm not sure what you were thinking of IA...
Steven Lord
Steven Lord am 16 Jul. 2024
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 am 16 Jul. 2024
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 15 Jul. 2024

0 Stimmen

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 Kommentar

L. F.
L. F. am 15 Jul. 2024
Bearbeitet: L. F. am 15 Jul. 2024
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024a

Tags

Gefragt:

am 14 Jul. 2024

Kommentiert:

dpb
am 16 Jul. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by