如何在MATLAB中检索函数参数的名称？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (162)

divide.m

```function [value, remain] = divide(left, right)
value = floor(left / right);
remain = left / right - value;
end```

2 个回答

• 不同数量的空白或空行，
• 存在单行注释或块注释，
• 声明分成多行。

(事实证明，解释一个块评论是最棘手的部分...)

```function [inputNames, outputNames] = get_arg_names(filePath)

%# Open the file:
fid = fopen(filePath);

defLine = '';
while all(isspace(defLine))
end

%# Collect all lines if the definition is on multiple lines:
index = strfind(defLine, '...');
while ~isempty(index)
index = strfind(defLine, '...');
end

%# Close the file:
fclose(fid);

%# Create the regular expression to match:
matchStr = '\s*function\s+';
if any(defLine == '=')
matchStr = strcat(matchStr, '\[?(?<outArgs>[\w, ]*)\]?\s*=\s*');
end
matchStr = strcat(matchStr, '\w+\s*\(?(?<inArgs>[\w, ]*)\)?');

%# Parse the definition line (case insensitive):
argStruct = regexpi(defLine, matchStr, 'names');

%# Format the input argument names:
if isfield(argStruct, 'inArgs') && ~isempty(argStruct.inArgs)
inputNames = strtrim(textscan(argStruct.inArgs, '%s', ...
'Delimiter', ','));
else
inputNames = {};
end

%# Format the output argument names:
if isfield(argStruct, 'outArgs') && ~isempty(argStruct.outArgs)
outputNames = strtrim(textscan(argStruct.outArgs, '%s', ...
'Delimiter', ','));
else
outputNames = {};
end

%# Nested functions:

if strcmp(strtrim(str), '%{')
strip_comment_block;
else
str = strtok([' ' str], '%');
end
end

function strip_comment_block
str = strtrim(fgets(fid));
while ~strcmp(str, '%}')
if strcmp(str, '%{')
strip_comment_block;
end
str = strtrim(fgets(fid));
end
end

end```

MATLAB提供了一种获取类元数据信息的方法(使用`meta`)，但是这仅适用于OOP类，而不是常规函数。

```classdef SomeTempClassName
methods
function [value, remain] = divide(left, right)
%# ...
end
end
end```

```function [inputNames,outputNames] = getArgNames(functionFile)
%# get some random file name
fname = tempname;
[~,fname] = fileparts(fname);

%# read input function content as string

%# build a class containing that function source, and write it to file
fid = fopen([fname '.m'], 'w');
fprintf(fid, 'classdef %s; methods;\n %s\n end; end', fname, str);
fclose(fid);

%# terminating function definition with an end statement is not
%# always required, but now becomes required with classdef
missingEndErrMsg = 'An END might be missing, possibly matching CLASSDEF.';
c = checkcode([fname '.m']);     %# run mlint code analyzer on file
if ismember(missingEndErrMsg,{c.message})
% append "end" keyword to class file
fid = fopen([fname '.m'], 'w');
fprintf(fid, '%s \n end', str);
fclose(fid);
end

%# refresh path to force MATLAB to detect new class
rehash

%# introspection (deal with cases of nested/sub-function)
m = meta.class.fromName(fname);
idx = find(ismember({m.MethodList.Name},functionFile));
inputNames = m.MethodList(idx).InputNames;
outputNames = m.MethodList(idx).OutputNames;

%# delete temp file when done
delete([fname '.m'])
end```

```>> [in,out] = getArgNames('divide')
in =
'left'
'right'
out =
'value'
'remain'```