How to overload display function properly?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
José-Luis
2013-1-8
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?
回答(2 个)
Jan
2013-1-8
Overloading display() is a bad idea. Any error or unexpected input can make Matlab unresponsive.
2 个评论
Daniel Shub
2013-1-8
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
2013-1-8
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.
Daniel Shub
2013-1-8
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!