# Strange behavior of evalin?

``````function a1 (x)
y = 1;
a2 (y);
end

function a2 (y)
evalin ("caller", "inputname(1)");
end
``````

Now, when calling

``````a1(x)
``````

I would expect the result `"x"` but I get `"y"`. Do I get something wrong or is this a bug?

It seems to be a regression bug. In Octave 5.2.0, one gets `"x"`. In Octave 6 and Octave 7 I indeed get `"y"`, which as you said, makes no sense to me either. Which version of Octave did you use?

Whatever this is, it is strange. I checked with Matlab and it also returns `"y"`. I made this slight modification:

``````z = 1;
a1(z)

function a1 (x)
inputname (1)
y = 1;
a2 (y);
end

function a2 (y)
inputname (1)
evalin ("caller", "inputname(1)");
end
``````

This prints `"z"`, `"y"`, `"y"` in Matlab. So it does appear that inputname is special and does not evaluate as expected.

1 Like

You are right @rik , I forgot about testing in Matlab before judging this seemingly strange behavior as bug (it seems to be a Matlab feature ).

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.
Variable name of function input - MATLAB inputname (Tips section)

Kai, Rik, thanks for testing (and for the idea about looking at Matlab). The regression broke the generation of the legend in some frequency plots in the control package. Therefore I was sure that this was supposed to work in earlier versions of Octave.

Should we leave it as it is (compatible to Matlab) or should this be regarded as bug (regradless the behavior in Matlab)?

I would proceed down two paths.

1. Re-code control package to avoid `inputname()`. This may not be possible, but this function is not high performance and it looks deeply in to the interpreter and seems rather fragile and subject to breaking in the future as the internals of the interpreter change.

2. Check the difficulty of supporting `evalin` and `inputname` together. I agree with you that the way that Octave used to behave is the way I would expect the two functions to interact together. However, this is such an uncommon scenario that I wouldn’t want to spend a lot of developer time fixing it. Hence, if it is easy we could do it, but if it looks at all hard then we should just document it as a strange Matlab behavior that Octave also emulates.

Sounds good.

The legends in the control packages are fixed. However, I have still used `inputname`, but only in the function of the related inputs itself, i.e., without `evalin`.

I think this might be more about the way evalin works and not specific to inputname. If you define the following functions in Matlab, what happens when you have some variables in the top-level workspace and you call f(), g(), and h()? Do you get the results you expect?

``````function f ()
evalin ('caller', 'who')
end
function g ()
gvar = 13;
f ()
end
function h ()
hvar = 42;
evalin ('caller', 'f')
end

>> topvar = 0;
>> f ()  %% Expect 'topvar' in list of vars from 'who'
>> g ()  %% Expect 'gvar' in list of vars from 'who'?
>> h ()  %% What happens here? Octave says 'hvar'.
``````

If Matlab behaves differently, then report that as a bug. If they both behave the same, then I’ll have a look at precisely what is happening there and try to explain it.

Matlab says `'topvar'` for `h ()`. I’ll file a bug report.