How can I obtain my expected result?
2 ビュー (過去 30 日間)
古いコメントを表示
I typed this code
function out=picker(c,in1,in2)
if c
out=fprintf('%d\n'in1);
else
out=fprintf('%d\n'in2);
end
my desire was to get this reslut
>>out=picker(true,5,6)
out=5
>>out=picker(false,5,6)
out=6
but matlab found a bug here .What is wrong with my code and how can I obtain my expected result?
0 件のコメント
採用された回答
claudio
2020 年 6 月 2 日
It depends on what type of outcome you need. If you need numeric out
function out = picker(c,in1,in2)
out = in2;
if c, out = in1; end
If you need char outcame
function out = picker(c,in1,in2)
out = sprintf('out = %d',in2)
if c, sprintf('out = %d',in1); end
その他の回答 (2 件)
David Hill
2020 年 6 月 2 日
function out=picker(c,in1,in2)
if c
out=in1;
else
out=in2;
end
fprintf('out=%d',out);
0 件のコメント
Image Analyst
2020 年 6 月 2 日
fprintf() returns the number of characters printed, which is not what you want. What you want is this:
function out = picker(c, in1, in2)
v = [in1, in2];
out = v(c+1);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!