Hello,
I am new to matlab and I have been having this problem:
-Say for example I have this function,
function [circle_area] = calcarea(r)
%calculates the area of a circle given radius length
circle_area = pi.*(r.^2)
end
- This displays out the output circle_area and also the "ans"
- I want to suppress the "ans: but without removing the output [cicle_area]
- If I delte [circle_area] I am affecting all my scripts where I use this function
- Example Script affected by deleting output:-
% This script calculates the area of a circle
% It prompts the user for the radius
radius = input('Please enter the radius: ');
% It then calls our function to calculate the
% area and then prints the result
area = calcarea(radius);
disp('The area is ');
disp(area);
- This displays an error too many outputs which I concluded came from the missing output when I delete [circle_area]
Can someone help me with this, it would be much appreciated.

 Respuesta aceptada

Stephan
Stephan el 12 de Nov. de 2020
% This script calculates the area of a circle
% It prompts the user for the radius
radius = input('Please enter the radius: ');
% It then calls our function to calculate the
% area and then prints the result
area = calcarea(radius);
fprintf('The area is: %f\n',area);
function circle_area = calcarea(r)
%calculates the area of a circle given radius length
circle_area = pi.*(r.^2);
end

3 comentarios

Ahmed Elsawah
Ahmed Elsawah el 12 de Nov. de 2020
Thanks for the answer. The thing is, this way round the function outputs ans = 12.566 for example instead of circle_area = 12.566.
Stephen23
Stephen23 el 12 de Nov. de 2020
Editada: Stephen23 el 12 de Nov. de 2020
"The thing is, this way round the function outputs ans = 12.566 for example instead of circle_area = 12.566"
Nope. This answer does what you asked for, no ans anywhere to be seen. Note that the name of the output variable circle_area used inside the function is totally irrelevant when you call the function.
% This script calculates the area of a circle
radius = 2;
area = calcarea(radius);
fprintf('The area is: %f\n',area);
The area is: 12.566371
function circle_area = calcarea(r)
%calculates the area of a circle given radius length
circle_area = pi.*(r.^2);
end
The only way you would get ans is if you did not use the code given in that answer, e.g. by calling the function without allocating its output to a variable and without suppressing its output with a semi-colon.
Ahmed Elsawah
Ahmed Elsawah el 12 de Nov. de 2020
Thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Nov. de 2020

Comentada:

el 12 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by