Effacer les filtres
Effacer les filtres

varagin with original var names

3 vues (au cours des 30 derniers jours)
Polsak Tothong
Polsak Tothong le 1 Juil 2023
Modifié(e) : Stephen23 le 1 Juil 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 commentaire
Stephen23
Stephen23 le 1 Juil 2023
Modifié(e) : Stephen23 le 1 Juil 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.

Connectez-vous pour commenter.

Réponse acceptée

Paul
Paul le 1 Juil 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

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by