I need to make a table that has column names "Radii", "SurfaceA", and "Volume". But when I display the table it names the columns "Var1", "Var2", and "Var3". What is causing this?
Im also brand new to Matlab, if there's some easy way to optimize this please let me know.
Radii = [0.050:0.050:0.300];
SurfaceA = [];
Volume = [];
for i = 1:length(Radii)
SurfaceA(i) = 4*pi*Radii(i)^2;
Volume(i) = (4*pi*Radii(i)^3)/3;
end
Sphere = table(Radii',SurfaceA',Volume');
disp("#1")
disp("Sphere = ")
fprintf("\n")
disp(Sphere)
This returns
#1
Sphere =
Var1 Var2 Var3
____ ________ _________
0.05 0.031416 0.0005236
0.1 0.12566 0.0041888
0.15 0.28274 0.014137
0.2 0.50265 0.03351
0.25 0.7854 0.06545
0.3 1.131 0.1131
Should be
#1
Sphere =
Radii Surface A Volume
____ ________ _________
0.05 0.031416 0.0005236
0.1 0.12566 0.0041888
0.15 0.28274 0.014137
0.2 0.50265 0.03351
0.25 0.7854 0.06545
0.3 1.131 0.1131
I need to make a table that has column names "Radii", "SurfaceA", and "Volume". But when I display the table it names the columns "Var1", "Var2", and "Var3". What is causing this?
Im also brand new to Matlab, if there's some easy way to optimize this please let me know.
Radii = [0.050:0.050:0.300];
SurfaceA = [];
Volume = [];
for i = 1:length(Radii)
SurfaceA(i) = 4*pi*Radii(i)^2;
Volume(i) = (4*pi*Radii(i)^3)/3;
end
Sphere = table(Radii',SurfaceA',Volume');
disp("#1")
disp("Sphere = ")
fprintf("\n")
disp(Sphere)
This returns
#1
Sphere =
Var1 Var2 Var3
____ ________ _________
0.05 0.031416 0.0005236
0.1 0.12566 0.0041888
0.15 0.28274 0.014137
0.2 0.50265 0.03351
0.25 0.7854 0.06545
0.3 1.131 0.1131
Should be
#1
Sphere =
Radii Surface A Volume
____ ________ _________
0.05 0.031416 0.0005236
0.1 0.12566 0.0041888
0.15 0.28274 0.014137
0.2 0.50265 0.03351
0.25 0.7854 0.06545
0.3 1.131 0.1131
Share
Improve this question
edited 23 hours ago
Cris Luengo
60.6k10 gold badges73 silver badges129 bronze badges
asked yesterday
Nathaniel KulickNathaniel Kulick
111 silver badge1 bronze badge
New contributor
Nathaniel Kulick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1 Answer
Reset to default 1When you construct a table
, the table variables only pick up the names from the names of the input workspace variables if you don't perform an operation on those input workspace variables. (In that case, you're effectively passing in to the table
constructor unnamed temporary arrays).
You can either:
Operate on the arrays up-front and store them back in named variables that you pass directly to the
table
constructor, orRadii = Radii'; SurfaceA = SurfaceA'; Volume = Volume'; Sphere3 = table(Radii,SurfaceA,Volume)
Use the name-value argument
VariableNames=["Radii", "Surface A", "Volume"])
Sphere2 = table(Radii', SurfaceA', Volume', ... VariableNames=["Radii", "Surface A", "Volume"])