How to make output of a function available to script without function call?

11 views (last 30 days)
Hello Friends,
I have the following function in myFun.m file:
function f1 = myFun1(X,Y)
[f1,f2] = myFun2(X,Y); %It calls myFun2
end
For some reason, from my script file, I can call only
f1 = myFun1(X,Y);
but not
[f1,f2] = myFun1(X,Y);
It is because the way I have a complicated script code. Nevertheless, I want output f2 to be available in my script right after function call f1 = myFun1(X,Y); in my script file myScript.m.
For illustration purpose, suppose:
%Script file contains:
X = [1 2; 4 5; 6 7];
Y = [8 9; 10 11];
f1 = myFun1(X,Y); % Calling myFun1 from myScript
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function file myFun2:
function [f1,f2] = myFun2(X,Y)
f1 = X*Y; % 3x2 matrix
f2 = pdist2(X,Y); % 3x2 matrix
end
In this illustration I made output f2 to be 3x2, but it could be any size, for example a 3x2x4 type double.
I will appreciate any advice!

Accepted Answer

Image Analyst
Image Analyst on 29 Aug 2016
The file "myfun.m" is missing the function keyword. It should look like
function f1 = myFun(X,Y)
[f1,f2] = myFun2(X,Y); %It calls myFun2
end
And myfun only returns f1, not both f1 and f2. If you want it to return both, you have to list both on the function line
function [f1, f2] = myFun(X,Y)
[f1,f2] = myFun2(X,Y); %It calls myFun2
end
The script "myscript.m" has an extra "end" at the end of the file. It's a script, not a function so it should not have an end. Actually functions don't even need "end" statements, and I never use them.
myscript.m should look like
% Script file contains:
X = [1 2; 4 5; 6 7];
Y = [8 9; 10 11];
% Get only f1, not f1 and f2 from myFun1.
f1 = myFun1(X,Y); % Calling myFun1 from myScript
[f1,f2] = myFun2(X,Y)
f1 = X*Y; % 3x2 matrix
f2 = pdist2(X,Y); % 3x2 matrix
Note that myFun1() will not call myfun.m because it has a 1 at the end of it so you must have a myFun1.m file or else you'll get an error.
  2 Comments
hello_world
hello_world on 29 Aug 2016
Edited: hello_world on 29 Aug 2016
Thanks for your reply!
I accept that there were typos in my illustration. I fixed them. It was created fast just for illustration purpose, however, my actual code did not have any of these typos.
Secondly, yes, I could call myFun2 directly, as you mentioned in your example above, i.e.,
[f1,f2] = myFun2(X,Y);
However, I was looking for other way to get it in my script directly from myFun1 and not from myFun2 (perhaps global variable?). Sometimes actual problems are difficult to explain and need a whole lot of code posting here.
dpb
dpb on 29 Aug 2016
Well, you could use a global but that has much to recommend against it.
Simply put, you're asking for the impossible a magic connection that doesn't exist in the code to "just appear"...

Sign in to comment.

More Answers (1)

dpb
dpb on 29 Aug 2016
[f1, f2] = function myFun(X,Y)
You're missing the second output in the function definition. Without it, there's no way other than (ugh! assignin or double-ugh!! global; when myFun exits, f2 being local and not returned per your definition is destroyed, ne'er to be seen again...'til the next invocation, anyway, at which time the same thing happens all over again.
  4 Comments
hello_world
hello_world on 29 Aug 2016
Edited: hello_world on 29 Aug 2016
Please see my comment in response to your answer below to clarify better what I want in my code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!