Calling functions

I have written a MATLAB code say myfun.m which first line is
function y = myfun(x)
Now I want to call it in another code I am writing. I have 2 values x1 and x2 and I want my present code to compare myfun(x1) and myfun(x2) such that if myfun(x1) > myfun(x2) then it does something.
I tried writing
if (myfun(x1)>myfun(x2))
...
But MATLAB says
??? Output argument "y" (and maybe others) not assigned during
call to "/Users/user/Documents/MATLAB/compare.m>myfun".
What should I do?
Thanks.

回答 (2 件)

the cyclist
the cyclist 2012 年 1 月 19 日

0 投票

This works for me:
x1 = 2;
x2 = 1;
if myfun(x1) > myfun(x2)
disp('myfun(x1) is greater than myfun(x2)')
else
disp('myfun(x1) is not greater than myfun(x2)')
end
where myfun.m is:
function y = myfun(x)
y = x.^2;
end
Jan
Jan 2012 年 1 月 19 日

0 投票

The error message means, that for some branchs of the program myfun the output y is not defined. You can use the debugger to let Martlab stop, when the problem occurs:
dbstop if error
Then you can investigate the current calling stack and values of the variables.

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

タグ

質問済み:

2012 年 1 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by