How to overload display function properly?

조회 수: 11 (최근 30일)
Immanuel
Immanuel 2013년 1월 8일
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
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?
Immanuel
Immanuel 2013년 1월 8일
편집: Immanuel 2013년 1월 8일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2013년 1월 8일
Overloading display() is a bad idea. Any error or unexpected input can make Matlab unresponsive.
  댓글 수: 2
Daniel Shub
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
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
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.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by