How to get the input variable names of a function (as cell array)

Hi
this may be a stupid question.
I have a function and I would like to have the input variable names as a strings or cell arrays.
e.g.
I have a function:
function [name, data]=calc_torque(test#,motor1,motor2,engine1,engine2)
[name, data]=calc_torque(test#,varargin)
Within the function I do some calculations and I would also like to get a cell array of the input variable names:
name={'motor1', 'motor2',...}
I tried to do it with inputname(argument) but it only accepts a scalar as argument, So I would only get one name instead of an array of all.
Any help is apprecated
Thanks in advance
mart

댓글 수: 2

It's extremely difficult to see why you would want to do this. If you can explain your reason (i.e. the purpose of the function) it may be that there is some much better approach that avoids getting the variable names.
Well I get inputs arrays from combustion engines and E-motors. Depending on the testcell configuration I have more or less input arrays e.g sometimes I have 2 sometimes 20 or even more.
The functions basically just does some calculations and outputs a cell array of the data and a separate cell array with the names of the Emotor or combustion Engine.
The reason is that I need this format to export it to other optimization tools or sometimes to Excel...

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2012년 1월 31일
name = cell(nargin,1);
for K = 1 : nargin
name{K} = inputname(K);
end

댓글 수: 2

It might be more secure and flexible, if you define the input as a STRUCT. Then the wanted names are the fieldnames.

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2012년 1월 30일

0 개 추천

I know of no way to do this with varargin. If it's really important you could pass in a cell array of names as another input.

카테고리

도움말 센터File Exchange에서 Oil, Gas & Petrochemical에 대해 자세히 알아보기

질문:

2012년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by