varagin with original var names

2 次查看(过去 30 天)
Hi,
Is it possible to carry or parse original variable names into the function using varargin
tout = zProcess( aa, bb, cc)
function [ tdata] = zProcess( varargin)
% a way to parse varargin into aa, bb, cc
  1 个评论
Stephen23
Stephen23 2023-7-1
编辑:Stephen23 2023-7-1
"Is it possible to carry or parse original variable names into the function using varargin"
That would be a very bad way to write code:
The whole point of functions is that they are a black box: what happens inside should be irrelevant. For example, do you know what internal variable names the SQRT function uses? No, you don't. Imagine if SQRT only worked with one particular input variable name, that would be a horrendously user-unfriendly function. Ugh.
What you are proposing throws away most of the benefits of using functions. Why would you want to do that?
In any case, this question is a good example of
Instead of asking us about your attempted solution, you should describe what the actual goal is: what kind of data do you have and how do you need to process it? Forget about magically naming variables like that, unless you really want to force yourself into writing slow, complex, buggy, inefficient, obfuscated code that is hard to debug. Best avoided.

请先登录,再进行评论。

采纳的回答

Paul
Paul 2023-7-1
inputname can be used to return the name of the variable in the calling workspace as a character vector, at least for simple calling cases. What would you do with the results from inputname?
[aa,bb,cc] = deal(0);
tout = zProcess( aa, bb, cc);
s = 'aa'
s = 'bb'
s = 'cc'
function [ tdata] = zProcess( varargin)
for ii = 1:numel(varargin)
s = inputname(ii)
end
tdata = 1;
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by