Registration function parameters in MATLAB

I am trying to write a generic registration function for all input parameters passed to a function in MATLAB. Is it possible to easily transfer all input parameters to another function without individually specifying parameters? In the logging function, I can of course use inputname(i)in a for loop to get the parameter names. I would prefer not to execute this logic in the main function. So, is there a way to do something like LogParams(allInputParams)?

+5
source share
1 answer

It looks like you have a main function, and from this function you want to call a function LogParamsto get a list of variable names passed to the main function. Here is one way to implement your function LogParams:

function names = LogParams
  names = evalin('caller','arrayfun(@inputname,1:nargin,''UniformOutput'',0)');
end

The result returned from LogParamswill be an array of cells containing the names of the variables passed to the calling function LogParams. The above solution uses the following functions:

  • EVALIN : evaluate the expression in the workspace of the calling function.
  • ARRAYFUN : as an alternative to the for loop.
  • NARGIN : get the number of arguments passed to the function.
  • INPUTNAME : get the name of the input variable.

To illustrate how it works LogParams, create the following function that calls it:

function main_function(a,b,varargin)
  disp(LogParams);
end

( x, y z):

>> main_function(x,y)
    'x'    'y'

>> main_function(x,y,z)
    'x'    'y'    'z'

>> main_function(x,y,z,z,z,z)
    'x'    'y'    'z'    'z'    'z'    'z'
+8

All Articles