How to find line of code that prints ans

107 ビュー (過去 30 日間)
RuiQi
RuiQi 2016 年 6 月 20 日
編集済み: per isakson 2019 年 11 月 2 日
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
  1 件のコメント
per isakson
per isakson 2016 年 6 月 20 日
Good question!

サインインしてコメントする。

回答 (3 件)

John D'Errico
John D'Errico 2016 年 6 月 20 日
編集済み: John D'Errico 2016 年 6 月 20 日
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 件のコメント
Jesse Hopkins
Jesse Hopkins 2019 年 10 月 30 日
編集済み: per isakson 2019 年 11 月 2 日
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
Stephen23 2016 年 6 月 20 日
編集済み: Stephen23 2019 年 5 月 24 日
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 2016 年 6 月 20 日
If you skim through.....the one without semi colon will be highlighted at '=' with underline red....
  2 件のコメント
Dave
Dave 2019 年 5 月 24 日
編集済み: Dave 2019 年 5 月 24 日
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 2019 年 5 月 24 日
編集済み: Stephen23 2019 年 5 月 24 日
"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"...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by