Output displaying from fprintf command via a function

My output is displaying from the fprintf command via my function and I would like to supress that
function [E]=energy
clc
close all
format compact
c=2.9979*10^8; % speed of light
disp('Please enter the numbers in square brackets')
disp('or use the colon operator')
mass=input('Please give me the vector of mass in kilograms= ');
m=transpose(mass);
E=m*c.^2;
fprintf('The corresponding energy from the mass vector \n of mass is %g joules.\n', E)

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 3 月 15 日

0 投票

When you run the function, in the command window (or in a script) enter the command
E = energy;
with the semi-colon. The semi-colong says not to print out the result of the function call.

3 件のコメント

Miya Harris
Miya Harris 2022 年 3 月 15 日
Thank you! Is there a way to implement that into my function?
Walter Roberson
Walter Roberson 2022 年 3 月 15 日
The way to implement that within your function is to not return anything from the function (which would require not assigning the output to anything when the function is called.)
That display of results happens after the function has completely returned, so the function cannot have any influence on that display of results.
Miya Harris
Miya Harris 2022 年 3 月 15 日
ok, thank you.

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

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

2022 年 3 月 15 日

コメント済み:

2022 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by