Stop MATLAB from print ans, and just display z

17 ビュー (過去 30 日間)
Alice K
Alice K 2023 年 2 月 8 日
回答済み: Shubham 2023 年 2 月 8 日
Hi, I am making a function that will take 2 imputs and sub them into an equation.
When I use the fucntion it displays Z and also prints ans, How do I stop the ans from being printed.
function [Z] = Company(B1,B2)
Z = 2*B1 + 3*B2;
disp(Z)
end
Thank you

採用された回答

Sarvesh Kale
Sarvesh Kale 2023 年 2 月 8 日
When you make a call to Company then the disp function will execute and it will also return a value Z, if the Company function does not have a lvalue to it, it will print the returned Z value, to avoid that use a semicolon after function call
Company(3,4) % this will print the ans
%
Company(3,4); % this will not print the ans
a = Company(3,4); % even this will not print ans
I hope this answers your queries.
  1 件のコメント
Alice K
Alice K 2023 年 2 月 8 日
It does, Thank you!

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

その他の回答 (1 件)

Shubham
Shubham 2023 年 2 月 8 日
Hi Alice, If you don't want to get `ans` variable to be printed in the command window. You can try this code:
function Company(B1,B2)
Z = 2*B1 + 3*B2;
disp(Z)
end
In your code, you have taken Z variable as output argument. So, let's say if you are calling your function like Company(5,6) then this command gives 28 and ans=28 because by default it creates ans variable to be in place of Z variable that you have declared as output argument. Instead of doing this, if you would write Z=Company(5,6), this will give you 28 and Z=28. Hope this clarify your doubt!

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by