Clear Filters
Clear Filters

varagin with original var names

4 views (last 30 days)
Polsak Tothong
Polsak Tothong on 1 Jul 2023
Edited: Stephen23 on 1 Jul 2023
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 Comment
Stephen23
Stephen23 on 1 Jul 2023
Edited: Stephen23 on 1 Jul 2023
"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.

Sign in to comment.

Accepted Answer

Paul
Paul on 1 Jul 2023
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

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!