Write a script that will loop to prompt the user for n circle radii. The script will call a function to calculate the area of each circle, and will write the results in sentence form to a file
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:9| Question number:27.9
All Answers
total answers (1)
Ch9Ex27.m
% Write the areas of circles to a file
% Prompt the user for the radii
n = 5;
%Open a new file for writing
fid = fopen('circles.dat','w');
for i = 1:n
radius = input('Enter the radius of a circle: ');
while radius <= 0 %Error-check user input
radius = input('Enter the radius of a circle: ');
end
area = calcarea(radius);
%Write to file "circles.dat"
fprintf(fid,'The area of circle #%d is %.2f\n',i,area);
end
%Error-check file close
closeresult = fclose(fid);
if closeresult == 0
disp('File close successful')
else
disp('File close not successful')
end
need an explanation for this answer? contact us directly to get an explanation for this answer