Read the original output variable of function

2 次查看(过去 30 天)
% 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 个评论
Stephen23
Stephen23 2022-7-20
"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 2022-7-20
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?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2022-7-20
编辑:Jan 2022-7-20
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.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by