passing 'varagin' and getting inputname() to work properly

2 ビュー (過去 30 日間)
Adam
Adam 2012 年 12 月 21 日
Is it possible to pass 'varargin' to an internal function and still get inputname() to return the original variable names?
e.g.
function struct = temp( varargin )
struct = parseInputs( varargin{1:end} )
end
function struct = parseInputs( varargin )
struct.name1 = inputname(1);
struct.name2 = inputname(2);
struct.name3 = inputname(3);
struct.name4 = inputname(4);
end
>> output = temp( a, b, c, d )
output =
name1: 'a'
name2: 'b'
name3: 'c'
name4: 'd'

採用された回答

Jonathan Sullivan
Jonathan Sullivan 2012 年 12 月 21 日
編集済み: Jonathan Sullivan 2012 年 12 月 21 日
You could use "evalin"
In your example it would be something like:
function struct = temp( varargin )
struct = parseInputs( varargin{1:end} )
end
function struct = parseInputs( varargin )
for ii = 1:length(varargin)
struct.(['name' num2str(ii)]) = evalin('caller',['inputname(' num2str(ii) ');']);
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by