Input and Output Arguments
2 次查看(过去 30 天)
显示 更早的评论
I have 4 functions all with multiple input and out put arguments. These functions genrate multiple tables in my Workspace.
I would like to create a "wrapper function" which does not require the statement of all input and output arguments without lossing the individual tables generated in my workspace.
Function 1 first line:
function [Data,ACR,SACW,ACERE,ACTarget,ACTolerance,ERETolerance,EREIterations,ERELimits] = EREOP1()
Function 2 first line:
[EREDrift,DrawDown,InformationRatio,RollStdDev,RollCorrelation,Percent,NAV,BOD,ACV,ACR,ERETarget,EREContrib,ERETotal,BmkRtnContrib,CummBmkRtnTotal,RtnContrib,CummRtnContribTotal,ActiveRtnDrift,CummActiveRtnDrift,CummACRtn,ACVNRB,NAVNRB,PercentNRB,ERELimit,Roll12MthActiveRtn,LagCummActiveRtn] = EREAnalysis1(SACW,ACR,ACTarget,ACTolerance,ACERE,ERELimits,EREIterations)
It is really important individual tables generate in my workspace as I call this from a wrapper function.
0 个评论
回答(1 个)
Walter Roberson
2022-5-7
If you were to use a non-anonymous function to do that, then the function would have to use evalin('caller') and assignin('caller'). This is not recommended.
Anonymous functions would have to use the same tricks, but it would be difficult to code well.
Exception: if you are willing to use the values of those variables as of the time you define the function, then anonymous functions could "capture" the values. But it would be tricky to write.
If you want to be able to run something that magically uses particular variable names from the workspace without passing the values in, and wants to magically write to particular names without them appearing on the left side of the = then that is what scripts are for.
2 个评论
Steven Lord
2022-5-7
I do not want to have to copy a massive list of input and out put arguments each time I run a function.
That's a good idea. Are these input or output arguments related to each other in some way, such that you really want to keep them together in one place? In that case I'd suggest packing some or all of the outputs into a struct array or an object. Instead of defining three variables, for instance:
x = 1;
y = 2;
z = 3;
you could store them in a struct.
s = struct('x', 1, 'y', 2, 'z', 3);
s is one variable that contains 3 pieces of data while x, y, and z are three variables that each contains 1 piece of data.
whos
Compare:
q1 = fun1(s)
q2 = fun2(x, y, z)
function result = fun1(A)
result = A.x + A.y.^A.z;
end
function result = fun2(x, y, z)
result = x + y.^z;
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!