Inputname statement - useful example

조회 수: 1 (최근 30일)
Gennaro Arguzzi
Gennaro Arguzzi 2017년 5월 29일
댓글: Walter Roberson 2017년 5월 29일
Hi everyone,
could you help me to understand what is the useful of the inputname statement? I run the following example:
function c = somma(a, b)
disp(inputname(1))
c = a + b;
in workspace:
x = 1;
y = 2;
somma(x, y) %x is the result
From my example is not clear the useful of inputname. If you have a best example, let me know please.
Thank you.
  댓글 수: 1
Stephen23
Stephen23 2017년 5월 29일
편집: Stephen23 2017년 5월 29일
In general it is not useful: the names that variables have inside and outside of functions should be totally irrelevant from outside and inside respectively.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 29일
inputname() is useful for giving error messages that reference variable names in the form the user knowns them.
  댓글 수: 4
Gennaro Arguzzi
Gennaro Arguzzi 2017년 5월 29일
Walter, unfortunately matlab shows the following errors:
tryme(1)
Error using exist The optional second input to exist must be 'var', 'builtin', 'class', 'dir' or 'file'.
Error in tryme (line 2) if ~exist('var1', 'variable') isempty(var1) ndims(var1) ~= 3
Walter Roberson
Walter Roberson 2017년 5월 29일
function tryme(var1)
if ~exist('var1', 'var') || isempty(var1) || ndims(var1) ~= 3
varname = inputname(1);
if isempty(varname); varname = 'first input'; end
error('%s must be a 3D array', varname);
end

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

추가 답변 (2개)

Jan
Jan 2017년 5월 29일
The command inputname is not useful. The name of a variable should not matter. Some inputs do not even have a name, e.g. in: somma(1, 2) or somma(x+1, y+2).
There are some rare situations, where the name is useful, e.g. when you want to display a variable.
I'm programming for many years now and was never faced with the need of using inputname. If the name of a value matters, I create a struct, which carries the name in an extra field.

dpb
dpb 2017년 5월 29일
I don't know as I have a directly useful real example on hand, and in general functions shouldn't rely on needing outside knowledge for their use, but one could imagine places in which what the function should do depends on what the variable passed is.
A totally generic routine for reading some data could by this mechanism create a labeled output of the inputs if it were called with different arguments at different points in the code. One could envision this particularly in cases using the dreaded eval in the calling routine so that the calling routine actually is also variable.
I would NOT recommend this as a common practice; it's there for those rare situations that might call for it.
  댓글 수: 2
Stephen23
Stephen23 2017년 5월 29일
"One could envision this particularly in cases using the dreaded eval in the calling routine"
The inputname documentation explicitly warns against this: "Avoid using inputname in the critical path of code or to obtain variable names to be used with commands such as eval, evalin, and assignin. This use of inputname can lead to code that is difficult to maintain."
dpb
dpb 2017년 5월 29일
Oh, yeah, did say it is "the dreaded eval" and I meant that explicitly, NOT tongue-in-cheek. I was just trying to come up with a reason why one could need to retrieve a name from the caller in the callee, not recommending same.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by