How to overload display function properly?

3 visualizzazioni (ultimi 30 giorni)
Immanuel
Immanuel il 8 Gen 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 Commenti
José-Luis
José-Luis il 8 Gen 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 il 8 Gen 2013
Modificato: Immanuel il 8 Gen 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

Accedi per commentare.

Risposte (2)

Jan
Jan il 8 Gen 2013
Overloading display() is a bad idea. Any error or unexpected input can make Matlab unresponsive.
  2 Commenti
Daniel Shub
Daniel Shub il 8 Gen 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 il 8 Gen 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.

Accedi per commentare.


Daniel Shub
Daniel Shub il 8 Gen 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.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by