【问题标题】:capturing variable names passed into a function in matlab捕获传递给matlab函数的变量名
【发布时间】:2014-01-15 15:17:00
【问题描述】:
有没有办法从新函数中获取传递给 matlab 函数的结构的名称。
例子:
y = fnX(StructName) % call function
function[y] = fnX(name)
% here is where I want to capture StructName as char text.
end
似乎结构被重命名了,原来的名字在函数中丢失了。有什么建议吗?
【问题讨论】:
标签:
matlab
function
parameter-passing
【解决方案1】:
我能给出的最佳建议是不要依赖变量的名称。
通过inputname 是可能的,但是将名称或您感兴趣的任何信息存储在变量中会更自然。例如,向结构中添加一个字段。
这应该允许更多的代码灵活性和可重用性,同时可能会提高可读性。
【解决方案2】:
您需要使用inputname。
function[y] = fnX(name)
% here is where I want to capture StructName as char text.
StructName = inputname(1);
end