Input and Output Arguments

4 views (last 30 days)
Tony Foley
Tony Foley on 7 May 2022
Commented: Steven Lord on 7 May 2022
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.

Answers (1)

Walter Roberson
Walter Roberson on 7 May 2022
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 Comments
Tony Foley
Tony Foley on 7 May 2022
Edited: Tony Foley on 7 May 2022
Hi, thanks for response.
What I am lookign for is not majic... this shoudl be the application of straight logic.
I do not want to have to copy a massive list of input and out put arguments each time I run a function. Hence, seeking to have a wrapper function so they are stated in code any way, or even stated in the same function.
Steven Lord
Steven Lord on 7 May 2022
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
Name Size Bytes Class Attributes cmdout 1x33 66 char s 1x1 528 struct x 1x1 8 double y 1x1 8 double z 1x1 8 double
Compare:
q1 = fun1(s)
q1 = 9
q2 = fun2(x, y, z)
q2 = 9
function result = fun1(A)
result = A.x + A.y.^A.z;
end
function result = fun2(x, y, z)
result = x + y.^z;
end

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!