How to overload display function properly?

3 views (last 30 days)
Immanuel
Immanuel on 8 Jan 2013
So here is the question ... but before that take a look at the following
Code:
a=3
Answer:
a =
3
So the = calls display function with 3 as input argument (eg. display(3)) however in the answer seen in the command window the line 'a =' is also displayed.
1. Does that mean the display function has another input argument which has the variable that the value was assigned to?
2. Or is there a way to get the name of the variable without explicitly passing the variable name to the function?
  2 Comments
José-Luis
José-Luis on 8 Jan 2013
Where did you get that = calls the display function? I very much doubt that's the case. Matlab will show the output of your code unless you finish every statement with a semicolon, that is default behavior.
How do you expect to get a variable without passing its name?
Immanuel
Immanuel on 8 Jan 2013
Edited: Immanuel on 8 Jan 2013
I got it there is a function called inputname that takes the name of the input and hence I am able to pass the name aswell

Sign in to comment.

Answers (2)

Jan
Jan on 8 Jan 2013
Overloading display() is a bad idea. Any error or unexpected input can make Matlab unresponsive.
  2 Comments
Daniel Shub
Daniel Shub on 8 Jan 2013
Can you provide an example? I just overloaded disp and display to throw an error for the double class and it doesn't seem to cause major problems.
Jan
Jan on 8 Jan 2013
It does not cause problems, when it does not cause problems. But when it causes problems, the debugging is much harder, when display() is failing.

Sign in to comment.


Daniel Shub
Daniel Shub on 8 Jan 2013
It seems there are three questions here:
1. Does that mean the display function has another input argument which has the variable that the value was assigned to?
No. The display function takes a single argument. The documentation is pretty clear and provides a "typical" example implementation of display
2. Or is there a way to get the name of the variable without explicitly passing the variable name to the function?
Yes, as you say in your comments that function is inputname.
How to overload display function properly?
I essentially asked this question under a different name. In that question I suggested two methods to overload display, both having drawbacks (either using eval or recreating the built-in display function). No better method was suggested.

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!