Help with using fprintf and matrices
8 views (last 30 days)
Show older comments
Oskar O'NEAL
on 11 Mar 2019
Commented: Image Analyst
on 15 Apr 2022
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!
0 Comments
Accepted Answer
Star Strider
on 11 Mar 2019
It is usually necessary to experiment.
Try this:
fprintf(formatspec,[R',SA',V']')
or equivalently:
fprintf(formatspec,[R; SA; V])
both producing:
Radius Surface Area Volume
0.00 in 0.000 in^2 0.000 in^3
0.25 in 0.785 in^2 0.065 in^3
0.50 in 3.142 in^2 0.524 in^3
0.75 in 7.069 in^2 1.767 in^3
1.00 in 12.566 in^2 4.189 in^3
1.25 in 19.635 in^2 8.181 in^3
1.50 in 28.274 in^2 14.137 in^3
1.75 in 38.485 in^2 22.449 in^3
2.00 in 50.265 in^2 33.510 in^3
2.25 in 63.617 in^2 47.713 in^3
2.50 in 78.540 in^2 65.450 in^3
2.75 in 95.033 in^2 87.114 in^3
3.00 in 113.097 in^2 113.097 in^3
that appears to be what you want.
4 Comments
Naomi Penelope
on 15 Apr 2022
Hello, after it is display as a matrix why is it not saving to the Workspace? I am having trouble saving the matrix, help please
Image Analyst
on 15 Apr 2022
@Naomi Penelope I don't understand. The variable has to exist in the workspace before you can use fprintf() to display the values in the command window. If you can't see the variable name in the workspace panel, then it doesn't exist and therefore you can't print it.
Why do you say you are having trouble saving the matrix to the workspace? Where does it exist before you try to "save it to the workspace"?
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.
See Also
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!