How to find line of code that prints ans

107 Ansichten (letzte 30 Tage)
RuiQi
RuiQi am 20 Jun. 2016
Bearbeitet: per isakson am 2 Nov. 2019
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

Antworten (3)

John D'Errico
John D'Errico am 20 Jun. 2016
Bearbeitet: John D'Errico am 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 Kommentar
Jesse Hopkins
Jesse Hopkins am 30 Okt. 2019
Bearbeitet: per isakson am 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

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 20 Jun. 2016
Bearbeitet: Stephen23 am 24 Mai 2019
Method one
The MATLAB editor already identifies (most of) these lines for you!
This is one of the many helpful features that the code analyzer has.
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
Here is one simple way using the excellent text editor Notepad++:
  1. open your file
  2. click Search -> Find
  3. on the find tab click "regular expression"
  4. in the "Find What" filed enter this string: [^;]$
  5. 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.

KSSV
KSSV am 20 Jun. 2016
If you skim through.....the one without semi colon will be highlighted at '=' with underline red....
  2 Kommentare
Dave
Dave am 24 Mai 2019
Bearbeitet: Dave am 24 Mai 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
Stephen23 am 24 Mai 2019
Bearbeitet: Stephen23 am 24 Mai 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"...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by