MATLAB : How to keep output values after using return
4 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Stephen23
on 17 Mar 2019
Edited: Stephen23
on 17 Mar 2019
"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 Comments
Stephen23
on 18 Mar 2019
Edited: Stephen23
on 18 Mar 2019
"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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!