Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine where a number is printing from in MATLAB?

Tags:

matlab

In MATLAB, how do you tell where in the code a variable is getting output?

I have about 10K lines of MATLAB code with about 4 people working on it. Somewhere, someone has dumped a variable in a MATLAB script in the typical way:

foo

Unfortunately, I do not know what variable is getting output. And the output is cluttering out other more important outputs.

Any ideas?

p.s. Anyone ever try overwriting Standard.out? Since MATLAB and Java integration is so tight, would that work? A trick I've used in Java when faced with this problem is to replace Standard.out with my own version.

like image 414
John Avatar asked May 14 '09 14:05

John


People also ask

How do you check output in MATLAB?

MEX functions can display output in the MATLAB® command window. However, some compilers do not support the use of std::cout in MEX functions. Another approach is to use std::ostringstream and the MATLAB fprintf function to display text in the MATLAB command window.

Is used in MATLAB for printing data in output screen?

The fprintf function is used for printing information to the screen. The fprintf function prints an array of characters to the screen: fprintf('Happy Birthday\n');

Is there a print function in MATLAB?

print( printer , driver ) specifies the printer and the driver. print( '-clipboard' , clipboardformat ) copies the current figure to the clipboard using the format specified by clipboardformat .

How do you print numbers in MATLAB?

There are three common ways: Type the name of a variable without a trailing semi-colon. Use the “disp” function. Use the “fprintf” function, which accepts a C printf-style formatting string.


1 Answers

Ooh, I hate this too. I wish Matlab had a "dbstop if display" to stop on exactly this.

The mlint traversal from weiyin is a good idea. Mlint can't see dynamic code, though, such as arguments to eval() or string-valued figure handle callbacks. I've run in to output like this in callbacks like this, where update_table() returns something in some conditions.

uicontrol('Style','pushbutton', 'Callback','update_table')

You can "duck-punch" a method in to built-in types to give you a hook for dbstop. In a directory on your Matlab path, create a new directory named "@double", and make a @double/display.m file like this.

function display(varargin)
builtin('display', varargin{:});

Then you can do

dbstop in double/display at 2

and run your code. Now you'll be dropped in to the debugger whenever display is implicitly called by the omitted semicolon, including from dynamic code. Doing it for @double seems to cover char and cells as well. If it's a different type being displayed, you may have to experiment.

You could probably override the built-in disp() the same way. I think this would be analagous to a custom replacement for Java's System.out stream.

Needless to say, adding methods to built-in types is nonstandard, unsupported, very error-prone, and something to be very wary of outside a debugging session.

like image 96
Andrew Janke Avatar answered Sep 19 '22 15:09

Andrew Janke