How can I pass global variables into a function?

How do I pass a variable name into a Matlab function called with cellfun?

  • So I know that myfun (http://www.mathworks.com/help/matlab/ref/inputname.html ) outputs the variable names that are passed into the myfun function (using the inputname(1) command). But when I pass variables through a cellfun, then the inputname(1) 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' 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.

  • Answer:

    cellfun(@(x)myfun(x,y),{j,lat,lon},'UniformOutput',false) Returns 'x' because the above defines a Anonymous function handle, which always inputs 'x' to the expression to its right, for more info, type the following on the MATLAB command prompt: >> help function_handle  cellfun(@myfun,{j},'UniformOutput',false) The documentation for INPUTNAME says :  If the input has no name, for example,     when it is the result of a calculation or an expression such as,     a(1), varargin{:}, eval(expr), etc, then inputname returns an     empty string. Which might very well be the case here. CELLFUN is probably calling "myfun" with some form of evaluated input, which does not have a name. Finally, to your real question of labelling plots with the variable name. Have you tried using the 'XLABEL' & 'YLABEL' commands? Both of them take strings as inputs, so after you the axis you want to print on, you should be able to use these functions with the appropriate inputs. Use the function WHOS to scrape off the names of variables in the workspace from which you want to call your functions with these variables. Here are the links to the documentation for the above functions: http://www.mathworks.com/help/matlab/ref/xlabel.html http://www.mathworks.com/help/matlab/ref/ylabel.html http://www.mathworks.com/help/matlab/ref/whos.html I would be happy to help out more, if you could share a more code which I could run. Cheers

Prabhakar Govind at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.