Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
inputname with structures?

Subject: inputname with structures?

From: Jeremy

Date: 31 Mar, 2011 21:37:04

Message: 1 of 3

Hi all. Small problem here. I've successfully used the inputname function in the past to retrieve the name of a variable being passed into a function. However, I just came across a problem where it doesn't appear to work when the input arguments are parts of a structure.

Example:

a.name = 'a'; %-- Simple structure
a.num = 1;

b.name = 'b'; %-- Simple structure
b.num = 2;

If I call
     temp = foo(a,b);
and inside of foo, I call inputname(1) and inputname(2) and get 'a' and 'b', respectively.

However, if instead I call
    temp = foo(a.name,b.name);
calling inputname(1) and inputname(2) within foo returns empty strings.

Can anyone suggest a quick workaround for this?

Subject: inputname with structures?

From: Jos (10584)

Date: 1 Apr, 2011 09:50:07

Message: 2 of 3

"Jeremy " <jeremy.brower@asu.edu> wrote in message <in2s60$6eb$1@fred.mathworks.com>...
> Hi all. Small problem here. I've successfully used the inputname function in the past to retrieve the name of a variable being passed into a function. However, I just came across a problem where it doesn't appear to work when the input arguments are parts of a structure.
>
> Example:
>
> a.name = 'a'; %-- Simple structure
> a.num = 1;
>
> b.name = 'b'; %-- Simple structure
> b.num = 2;
>
> If I call
> temp = foo(a,b);
> and inside of foo, I call inputname(1) and inputname(2) and get 'a' and 'b', respectively.
>
> However, if instead I call
> temp = foo(a.name,b.name);
> calling inputname(1) and inputname(2) within foo returns empty strings.
>
> Can anyone suggest a quick workaround for this?

INPUTNAME returns the name of the variable that is passed to the function. Since "a.name" is not a variable it will return empty; similar to the calling the function as foo('a','b').

I presume you would like inputname to return the string 'a.name'. Why do you want to know that? Perhaps there are some other alternative approached to that problem.

~ Jos
 

Subject: inputname with structures?

From: Steven_Lord

Date: 1 Apr, 2011 14:06:03

Message: 3 of 3



"Jeremy " <jeremy.brower@asu.edu> wrote in message
news:in2s60$6eb$1@fred.mathworks.com...
> Hi all. Small problem here. I've successfully used the inputname function
> in the past to retrieve the name of a variable being passed into a
> function. However, I just came across a problem where it doesn't appear to
> work when the input arguments are parts of a structure.
>
> Example:
>
> a.name = 'a'; %-- Simple structure
> a.num = 1;
>
> b.name = 'b'; %-- Simple structure
> b.num = 2;
>
> If I call
> temp = foo(a,b);
> and inside of foo, I call inputname(1) and inputname(2) and get 'a' and
> 'b', respectively.
>
> However, if instead I call
> temp = foo(a.name,b.name);
> calling inputname(1) and inputname(2) within foo returns empty strings.

That is the correct, expected, and documented behavior.

http://www.mathworks.com/help/techdoc/ref/inputname.html

"If the input argument has no name (for example, if it is an expression
instead of a variable), the inputname command returns the empty string
('')."

> Can anyone suggest a quick workaround for this?

Modify your code so it doesn't depend on the name of the input arguments or
pass the strings that you want to use as the INPUTNAME when the inputs are
expressions into the function as additional inputs.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us