How to find line of code that prints ans
61 views (last 30 days)
Show older comments
I have a very long code and there is one line that keeps printing ans = [ 1 1 1 255 ] but i cannot find it because code too big. How do i find the line that is printing this thing ? It prints it because i did not put semi colon
Answers (3)
John D'Errico
on 20 Jun 2016
Edited: John D'Errico
on 20 Jun 2016
The others have shown the classic ways to find an unterminated line, although it is still possible for them to fail if you were "creative" in your coding style. For example, if you create a line of text as a string, then use eval on it, they will not catch the event where your string lacks a semi-colon. Of course, using eval is often itself a sign of a poor coding style. (There are exceptions to that rule, where no other tool can do what you need. But even then, it is good to think about whether what you are doing is a bad idea. It usually is so.)
One idea I've seen before was to overload the display function.
display(17)
ans =
17
So, in general, whenever you see the "ans =" string appear, display created it, as above. So, if I just type the number 17 at the command line, MATLAB passes it to display to do the job.
17
ans =
17
If you have an unterminated line,like this:
x = 17
x =
17
Here display is also used, but it knows the variable name, so it gives a different output.
So, an idea is that one could overload display to allow the user to find those events. Then, with a wee bit of smart coding, one could set a flag to force display to also dump out the current stack where display was called from. I could see this done at the command line, where a simple command is used to set a preference. Something like these commands might suffice:
displayAnsMode stack
displayAnsMode normal
Thus the 'stack' option would force display to dump out the current stack, before passing the result to the normal display. It would only do that when an ans was going to be generated.
The 'normal' option would restore display to its normal behavior.
I could think of a few other options here. Just a passing thought, something I might even consider writing one day to put on the FEX. For now though, on my list of "round tuits". Anyway, this capability SHOULD be in MATLAB itself.
1 Comment
Jesse Hopkins
on 30 Oct 2019
Edited: per isakson
on 2 Nov 2019
I just tried this method, and I found that I had to take it a step further. The mystery print looked something like:
ans =
(1 x 3 struct array)
...
So I at least knew that the type that was being displayed was a struct. In order to overload display for struct, I needed to create an '@struct' directory and place my overloaded display into that directory. I was clued into this by inspecting the contents of the command
which display -all
Stephen23
on 20 Jun 2016
Edited: Stephen23
on 24 May 2019
Method one
The MATLAB editor already identifies (most of) these lines for you!
Assignments without a semi-colon are marked in the editor using a warning (an orange underscore). The appropriate lines are also indicated on the RHS of the editor by the scrollbar. (This is a good example of why it is a good idea to learn how to use the editor properly, and to pay attention to the warning and error messages that it displays).
Note that statements without assignment (e.g. a function call where the output is not allocated) are not highlighted (presumably as this is thought to be intentional).
Method two
- open your file
- click Search -> Find
- on the find tab click "regular expression"
- in the "Find What" filed enter this string: [^;]$
- click Find Next button.
This will find all lines that do not end with a semi-colon. It has a few important restrictions. It will not work:
- on lines of code that contain comments trailing the code.
- on lines with multiple executable statements.
- where there is trailing whitespace.
You could also do something similar using MATLAB itself.
Method three
If the code is relatively small you can simply enter debugging mode, and step through the code until the ans appears.
0 Comments
KSSV
on 20 Jun 2016
If you skim through.....the one without semi colon will be highlighted at '=' with underline red....
2 Comments
Dave
on 24 May 2019
Edited: Dave
on 24 May 2019
I've just discovered, that there are exceptions...
if not(any(eml_handles == parentHandle)) % Dont describe ports when the port is inside an EML
not(any(eml_handles == PresentHandle)) % Dont describe ports when the block is EML
The if statement has two conditions but i've missed the trailing tripple dot after the first condition meaning that the second condition is seen as a command and executes - returning a logical '1' or '0' with no indication where the "ans" is being driven from.
The corrected code is here:
if not(any(eml_handles == parentHandle)) && ... Dont describe ports when the port is inside an EML
not(any(eml_handles == PresentHandle)) % Dont describe ports when the block is EML
My guess would be that a function call without an assignemnt (equals sign) would result in a command window ans without any indication of where it is coming from.
Stephen23
on 24 May 2019
Edited: Stephen23
on 24 May 2019
"My guess would be that a function call without an assignemnt (equals sign) would result in a command window ans without any indication of where it is coming from."
Yes, that appears to be correct. This means that the warning message (marked by underlining the = assignment) is misleading, as it states
Terminate statement with semicolon to suppress output
but as far as I understand a function being called is also a "statement"...
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!