Filter löschen
Filter löschen

Read the original output variable of function

2 Ansichten (letzte 30 Tage)
W. Feng
W. Feng am 20 Jul. 2022
Kommentiert: Steven Lord am 20 Jul. 2022
% Assume the main function with m.file is invisible for user. we just know the name of m.file
% e.g
function [ y1, y2]=myfun(x1,x2)
y1=x1;
y2=x2
% or function output could be a, b,
function [ a, b]=myfun(x1,x2)
y1=x1;
y2=x2
% or function output could be m, n
function [ m, n]=myfun(x1,x2)
y1=x1;
y2=x2
The question is how to get the real output argument name from the main function of myfun.
  2 Kommentare
Stephen23
Stephen23 am 20 Jul. 2022
"The question is how to get the real output argument name from the main function of myfun."
In one way or another you would have to parse the file (either DIY or using some inbuilt tool, e.g. MLINT, HELP, or whatever else might possibly return such information). But this will be slow and fragile... and very smelly:
because the names of variables used inside a function should not matter outside of it. What process are you attempting, that relies on knowing the variable names inside some function? Most likely there is a simpler, more efficient approach.
Steven Lord
Steven Lord am 20 Jul. 2022
What process are you attempting, that relies on knowing the variable names inside some function? Most likely there is a simpler, more efficient approach.
I agree. Why do you care what the author of the code used for the internal variables in their function?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 20 Jul. 2022
Bearbeitet: Jan am 20 Jul. 2022
This is impossible. If the code is not visible for the user, the names of the variables are not visible also. But remember, that the output need not be a variable at all:
function varargout = myfun(x1, x2)
varargout{1} = x1;
varargout{2} = x2;
end
myfun could be a MEX file also. Then the outputs are accessed as plhs[0] and plhs[1] and they do not have names.
The need to access the names of variables is "meta-programming". This means, that you write Matlab code, which parses and executes Matlab code. Such indirections are a strong hibnt for the application of programming anti-patterns. These are techniques, which makes programming harder and more complicated than useful.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by