Borrar filtros
Borrar filtros

varagin with original var names

3 visualizaciones (últimos 30 días)
Polsak Tothong
Polsak Tothong el 1 de Jul. de 2023
Editada: Stephen23 el 1 de Jul. de 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 comentario
Stephen23
Stephen23 el 1 de Jul. de 2023
Editada: Stephen23 el 1 de Jul. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Paul
Paul el 1 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by