最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

matlab - Why are my table columns not named properly? - Stack Overflow

programmeradmin5浏览0评论

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.
Add a comment  | 

1 Answer 1

Reset to default 1

When 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:

  1. Operate on the arrays up-front and store them back in named variables that you pass directly to the table constructor, or

    Radii = Radii';
    SurfaceA = SurfaceA';
    Volume = Volume';
    Sphere3 = table(Radii,SurfaceA,Volume)
    
  2. Use the name-value argument VariableNames=["Radii", "Surface A", "Volume"])

    Sphere2 = table(Radii', SurfaceA', Volume', ...
        VariableNames=["Radii", "Surface A", "Volume"])
    
发布评论

评论列表(0)

  1. 暂无评论