Is there a good way to retrieve variable names of function inputs when the variables are cell indexed?

3 ビュー (過去 30 日間)
I have a function, say myfun(varargin) which accepts variable number of inputs. These input variables are also cell indexed.
Now, is there an elegant way of retrieving the variable names in the function? I did go through the documentation for inputname, but I could not find anything for cell indexed variables.
myfun(varargin)
nVarargs = length(varargin);
v=[];
for i = 1:nVarargs
v = [v; NameOfInputVariable];
end %for i = 1:nVarargs
end % myfun(varargin)
% Calling the function
num1.real = [1 2 3.4];
num2.imag = [5 3 1.9];
myfun(num1.real, num2.imag);

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 3 月 29 日
inputname() only works when variables are passed, not when expressions are passed, and your num1.real is an expression rather than a variable.
You would have to do something like,
num1_real = [1 2 3.4];
num2_imag = [5 3 1.9];
myfun(num1_real, num2_imag);
Having done that, you can use
function v = myfun(varargin)
nVarargs = length(varargin);
v = {};
for i = 1:nVarargs
v{i} = inputname(i); %notice it is the argument _number_
end
end
  2 件のコメント
Samarth Shivaswamy
Samarth Shivaswamy 2017 年 3 月 29 日
v is now a 1x2 cell with two empty char arrays in it.
Walter Roberson
Walter Roberson 2017 年 3 月 30 日
That would happen if you used num1.real and num2.imag like you were using before. You need to use unindexed unqualified variables to be able to get the names, like in the call I show.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by