Help with using fprintf and matrices
Show older comments
Hello!
So I am attempting to list the list the surface area and volume of a sphere when given the radius in matrix form. However I am having trouble with is utilizing the fprintf command correctly to list these values.
format compact
R = [0:.25:3];
SA = 4*pi*(R).^2;
V = (4/3)*pi*(R).^3;
fprintf('\n\n');
disp(' Radius Surface Area Volume');
formatspec = '%9.2f in%10.3f in^2%10.3f in^3 \n';
fprintf(formatspec,R',SA',V')
I am attempting to get the Radius, Surface Area, and Volume to be in their own seperate columns, but the output looks like this.
Radius Surface Area Volume
0.00 in 0.250 in^2 0.500 in^3
0.75 in 1.000 in^2 1.250 in^3
1.50 in 1.750 in^2 2.000 in^3
2.25 in 2.500 in^2 2.750 in^3
3.00 in 0.000 in^2 0.785 in^3
3.14 in 7.069 in^2 12.566 in^3
19.63 in 28.274 in^2 38.485 in^3
50.27 in 63.617 in^2 78.540 in^3
95.03 in 113.097 in^2 0.000 in^3
0.07 in 0.524 in^2 1.767 in^3
4.19 in 8.181 in^2 14.137 in^3
22.45 in 33.510 in^2 47.713 in^3
65.45 in 87.114 in^2 113.097 in^3
If anyone can help seperate them, I'd appreciate it!
Accepted Answer
More Answers (1)
Image Analyst
on 11 Mar 2019
Do you mean like this:
format compact
R = [0:.25:3];
SA = 4*pi*(R).^2;
V = (4/3)*pi*(R).^3;
fprintf('\n Radius\n')
fprintf('%9.2f in\n',R')
fprintf('\n Surface Area\n')
fprintf('%10.3f in^2\n',SA')
fprintf('\n Volume\n')
fprintf('%10.3f in^3 \n',V')
Each output is in it's own column and they are printed one after the other vertically.
1 Comment
Oskar O'NEAL
on 11 Mar 2019
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!