How to make output of a function available to script without function call?
이전 댓글 표시
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!
채택된 답변
추가 답변 (1개)
dpb
2016년 8월 29일
[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
hello_world
2016년 8월 29일
편집: hello_world
2016년 8월 29일
Image Analyst
2016년 8월 29일
Of course not. I showed you the correct way to declare it in my answer.
hello_world
2016년 8월 29일
편집: hello_world
2016년 8월 29일
dpb
2016년 8월 29일
Sometimes we just can't have what we want, sorry.
카테고리
도움말 센터 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!