Unwanted "ans" in my output

2 ビュー (過去 30 日間)
Xuande Zhang
Xuande Zhang 2018 年 10 月 19 日
コメント済み: Ashfaque Ahmed 2018 年 10 月 23 日
Hi all,
I know a lot of people already asked this, but still i cannot have the correct output.
In my function,
function [area, cr] = circle(r);
area = pi * r^2
cr = pi * 2 * r
end
Still the first output (area) will be populated after I added semi-colon at end of first line.
Thanks for your help in advance.
  2 件のコメント
Ashfaque Ahmed
Ashfaque Ahmed 2018 年 10 月 23 日
Dear Sir, This is working but only one value is showing i.e. area.
Ashfaque Ahmed
Ashfaque Ahmed 2018 年 10 月 23 日
>> y = circle(1)
y =
3.1416

サインインしてコメントする。

採用された回答

madhan ravi
madhan ravi 2018 年 10 月 19 日
編集済み: madhan ravi 2018 年 10 月 19 日
[area,cr]=circle(2) % calling of function with two outputs , here r is 2
function [area, cr] = circle(r);
area = pi * r^2; %semicolon here
cr = pi * 2 * r; %semicolon here
end
  4 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 22 日
編集済み: madhan ravi 2018 年 10 月 22 日
>>area =
12.5664
cr =
12.5664
>>
madhan ravi
madhan ravi 2018 年 10 月 22 日
this is my output in my command window

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 10 月 19 日
This is what you see when you write your code in matlab's editor
Can't you see the editor highlights that point out the issues with your code? If you hover over them, the editor even tells you how to fix them and offer to fix it for you. Why do you ignore it?
As the editor tells you, no semi-colon on the first line, but semi-colons on the 2nd and 3rd, so
function [area, cr] = circle(r)
area = pi * r^2;
cr = pi * 2 * r;
end
  3 件のコメント
Guillaume
Guillaume 2018 年 10 月 22 日
First, follow the instruction given above and do the fixes that the editor tells you to do. Your function code should be
function [area, cr] = circle(r)
area = pi * r^2;
cr = pi * 2 * r;
end
Once you've done that, when you call your function, you'll notice that you no longer get the area and cr output (!) but still get your unwanted ans
That particular problem is because you need to learn how to call functions. As documented ans gets created when you call a function without assigning its result to a variable. The proper way to call your function is:
[area, perimeter] = circle(somevalue)
and no more ans !
Xuande Zhang
Xuande Zhang 2018 年 10 月 22 日
Thanks for your help! It worked.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by