Code in script is printing to command window

8 visualizaciones (últimos 30 días)
Bernoulli Lizard
Bernoulli Lizard el 9 de Ag. de 2012
Comentada: James Van Zandt el 30 de En. de 2019
I am editing a program that someone else wrote, and when I run it, each time it prints a number to the command window. I would like to find what line of code is causing this, but I can not find it and the script is many pages long. I have tried scanning to see if any line is missing the semi colon supresion and have also used find to search for "disp(", but I did not find anything.

Respuesta aceptada

Star Strider
Star Strider el 9 de Ag. de 2012
Editada: Star Strider el 10 de Ag. de 2012
One option is to use the Editor debugging functions and set breakpoints. Set them and remove them down your code as necessary until you locate the area of the code where the number starts to display.
A similar approach is to insert a break or return statement at a particular point in your code and then see if the number displays. (Those statements will stop code execution at that line.) If the number displays, the statement producing it is before the return you inserted. I suggest you add something like
return % ==============================================
to your return statement so it will be easier to find later. If the number didn't display before that return, comment that return out (so you know where it was), and do the same thing several lines further down in your code. Keep doing this until you locate the area where the number starts to display. This will at least narrow your search. Then comment out particular lines of code until you locate the one that's displaying the number.
This approach is kludgy beyond description, but sometimes it's the only way to solve such problems.
Additional idea: if you can't find a line in your code that you know is displaying the number, check the code's dependencies with depfun. The original author of the code could have written a function, called by the code you have, that's displaying the number.
  1 comentario
James Van Zandt
James Van Zandt el 30 de En. de 2019
Star Strider's method is an example of a binary search, which lets you cut down the length of code by a factor of two for each editing step. To speed this up, insert something like this in many places:
warning(' ');
(Note the space between the quotes). You'll get a bunch of printouts like this:
Warning:
> In myfunction (line 2)
Warning:
> In myfunction (line 22)
Warning:
> In myfunction (line 44)
This should narrow the range. Repeat to narrow it further.

Iniciar sesión para comentar.

Más respuestas (1)

Daniel Shub
Daniel Shub el 9 de Ag. de 2012
Editada: Daniel Shub el 9 de Ag. de 2012
I can think of three ways of doing this.
  • Set break points to narrow down where the output occurs. This could involve a lot of trial and error and if the code takes a long time to run will be inefficient.
  • Overload disp and fprintf to be something like dbstack. Assuming the output is invoked by either of these two methods you will get the calling line number.
  • Use ECHO. The output will show you each line of code and what it produces.
I do not believe anyone starts their code with
clear all; close all; echo off;
anymore, so you should be pretty safe.
By the way I am wanting upvotes for finding a potentially useful way of reviving a long dead command

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by