MATLAB : How to keep output values after using return

조회 수: 4 (최근 30일)
Somayyah Mohammed
Somayyah Mohammed 2019년 3월 17일
편집: Stephen23 2019년 3월 18일
I have this code segment:
function [S,r1,r2,c1,c2] = xor2imgs(org,templete,Diff)
while (r+O - 1) <= N
while (c+P - 1) <= M
if sum_temp <= sum(sum(sum(org(r:r+O-1,c:c+P-1)))) %cond #1
matrix = org;
matrix(r:r+O-1,c:c+P -1) = org(r:r+O-1,c:c+P-1)-templete(1:O,1:P);
mat_sum = sum(sum(sum(matrix)));
diff2 = org_sum-mat_sum;
if (mat_sum == diff) && (diff2 > Diff) %cond #2
S = org_sum-mat_sum;
r1 = r;
r2 = r1+O-1;
c1 = c;
c2 = c1+P-1;
return ; %must return to the original function yet keep the output values
end
end
end
end
end
What should I do to pass the values of [S,r1,r2,c1,c2] to the caller function after using return?

답변 (1개)

Stephen23
Stephen23 2019년 3월 17일
편집: Stephen23 2019년 3월 17일
"What should I do to pass the values of [S,r1,r2,c1,c2] to the caller function after using return?"
Nothing special at all. As long as those variables have been defined inside your function by the time you call return (or the function reaches its end) then those variables can be returned as output arguments. You just need to call that function with as many output arguments as you require:
[S_out,r1_out,r2_out,c1_out,c2_out] = xor2imgs(...)
How to call function with multiple output arguments is explained in the introductory tutorials:
  댓글 수: 2
Somayyah Mohammed
Somayyah Mohammed 2019년 3월 17일
I tried this but it keeps returning zero to each output....
Stephen23
Stephen23 2019년 3월 18일
편집: Stephen23 2019년 3월 18일
"I tried this but it keeps returning zero to each output...."
If you get zeros for each output then you have correctly passed (some) variables as output arguments. If you do not expect zero values then possibly your algorithm has a bug in it (e.g. wrong variables or wrong calculation) or you are not checking the data properly (e.g. you are looking in the wrong workspace). As you have not shown us how you call this function or how those variables are defined, it is difficult to be more specific than that: use the debugging tools to figure out why you get zero values. If you want more help show us exactly how you call this function and provide all values that we need to run it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by