-1

I know that the custom function myfun outputs the variable names that are passed to it using the inputname command.

However, when I pass variables through a cellfun the inputname command ceases to work, as shown below (where I am passing in the variable names j, lat, and lon. Here, it just somehow outputs all the variables as 'x'.

>> cellfun(@(x)myfun(x, y), {j, lat, lon}, 'UniformOutput', false)

First calling variable is "x".
First calling variable is "x".
First calling variable is "x".

ans = 
    'x'    'x'    'x'

And if I try:

cellfun(@myfun, {j}, 'UniformOutput', false)

I get:

First calling variable is "".

ans = 
    {''}

I'm doing this because I'm creating a bunch of plots with a function, and would like all the plots auto-labeled with the variable name of the variable I'm passing into the plots.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
InquilineKea
  • 891
  • 4
  • 22
  • 37
  • 2
    Instead of showing us 10 different ways to call your home-built function, why don't you show us that function instead? You should always try to give a minimum example that shows your problem, this question can be asked much better. – Bas Swinckels Sep 16 '13 at 06:05
  • Okay, I totally revamped the question now. – InquilineKea Sep 16 '13 at 06:20
  • I wouldn't recommend depending on variable names too much. It is nice to do this for a quick evaluation, but think twice before building it into a program. If you want to combine textual information with values consider using a `struct` for example. – Dennis Jaheruddin Sep 16 '13 at 13:39

1 Answers1

0

You lose the connection between the name of the variable you pass and inputname in this scenario. The only solution I see is to send the name of the variable as an additional argument to myfun. You need to extract the names using a separate function like this:

function [values, names] = getNames(varargin)
values = varargin;
names = cell(size(varargin));
for iArg = 1:nargin
    names{iArg} = inputname(iArg);
end
end

Then your code should look like this

[values, names] = getNames(j,lat,lon);
cellfun(@(x,n)myfun(x,y,n),values,names,'UniformOutput',false);

Now the third argument passed to myfun contains the name of its first argument. Update: This means you need to edit myfun to accept a third argument as the name of the variable instead of using inputname.

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
  • @EitanT Have you edited `myFun` to take the third argument as the name of the first argument instead of using `inputname`? – Mohsen Nosratinia Sep 16 '13 at 10:44
  • @EitanT The point is that you lose the name of variables when you create the cell array `{j, lat, lon}`. So the names should be captured before that. AND, probably `myfun` does some thing more, it would be pointless if the only thing `myfun` does is returning the name of it's argument. – Mohsen Nosratinia Sep 16 '13 at 11:17
  • `myfun` is probably copied from the example [here](http://www.mathworks.com/help/matlab/ref/inputname.html). If you are allowed to modify `myfun`, your workarounds seem pointless to me. You can print the variable names separately, and leave the rest of the calculations in `myfun`. – Eitan T Sep 16 '13 at 12:20
  • Unless the `myfun` function includes the plotting part that needs the name of the variables. I don't think the priniting of the variable names is the goal. They seem to be used for debugging. – Mohsen Nosratinia Sep 16 '13 at 13:03
  • Unless `myfun` IS the plotting function which you cannot modify. – Eitan T Sep 16 '13 at 13:04
  • OP says custom function, not built-in MATLAB function – Mohsen Nosratinia Sep 16 '13 at 13:05
  • This is due to my edit, to make it clear that in the current example `myfun` utilizes `inputname`. In the general case, I infer that `myfun` can be anything. – Eitan T Sep 16 '13 at 13:08