How do I change column headers in my table

I have some data that I am displaying in my script and have outputted it in 28 different tables. My script is quite long so I am just posting a screenshot for now.
Where is says var 2 I want to change to the storey number. So for table 1 I want storey 1, for table 2, storey 2 etc...
If you can see for the main output i am running a loop and using the matrix called "Int_force". I also have made the row headers as [Axial Force, Shear Force, Bending Moment. Thus, I am hoping someone can help me so that I can change the column headers to my desired wish? I am not sure how I would change the loop to accomplish this.
Many thanks,
Scott

1 Comment

You overwrite tables 1-27 in your loop and you are left with table 28.

Sign in to comment.

 Accepted Answer

If your immediate problem is naming the variables, perhaps --
Int_force = randn(6,6,28);
Force = repmat(["Axial Force";"Shear Force";"Bending Moment"],2,1);
for ii = 1:28
T{:,ii} = table(Force,Int_force(:,:,ii), VariableNames=["Force","Storey "+ii]);
end
T{1:3}
ans = 6×2 table
Force Storey 1 ________________ ____________________________________________________________________ "Axial Force" 0.25824 -1.0666 0.52455 0.061362 0.83612 -0.57872 "Shear Force" -0.33978 -0.677 0.08896 -0.55037 -1.9187 0.052839 "Bending Moment" -0.68629 -0.77981 0.52746 -0.6832 0.45266 0.13577 "Axial Force" 0.97137 2.5555 0.16246 1.1639 1.7025 -0.40126 "Shear Force" -1.3975 -0.23952 -2.0119 1.1432 -0.26792 2.2068 "Bending Moment" -1.4253 0.063041 -2.4109 0.26882 -0.90365 0.049716
ans = 6×2 table
Force Storey 2 ________________ _________________________________________________________________________ "Axial Force" -1.1477 0.6543 -0.14317 -0.039486 1.4339 -1.264 "Shear Force" -0.42278 -0.054125 -0.022768 0.5618 -0.078847 0.91655 "Bending Moment" -2.3065 -0.2194 -0.25508 1.2181 -0.1286 -1.2847 "Axial Force" -2.0327 1.8675 -2.0148 0.52304 0.49065 0.072621 "Shear Force" -0.29637 0.082269 1.2084 0.72773 2.039 -1.5388 "Bending Moment" -0.24284 -2.3689 -0.67396 0.36504 -0.58589 0.82584
ans = 6×2 table
Force Storey 3 ________________ _________________________________________________________________________ "Axial Force" 0.45866 -0.43073 0.18425 -0.22845 0.81357 -1.0366 "Shear Force" 0.39192 -0.052734 0.6977 0.09103 -0.42306 -1.1206 "Bending Moment" 0.061773 -0.91316 0.651 0.23485 -0.11815 -1.2856 "Axial Force" 2.1351 0.69848 -0.19996 -0.77539 2.8288 0.31379 "Shear Force" 1.9178 -1.041 0.0087346 -1.2634 0.10913 -0.33258 "Bending Moment" 0.31495 -1.8692 0.97796 -0.82828 -0.8282 -0.29363
Note that 'T' was being overwritten in every iteration of the loop, so I created a cell array of them.
Make appropriate changes to get the result you want.
.

4 Comments

Thanks, Star Strider! That looks great.
To go on a little bit further, is there a way perhaps of labelling the columns also? So under the Storey header I can have col 1, col 2, col 3 ,col 4, col 5,col 6?
That can be done.
Note that you cannot write the resulting table arrays using writetable and then read them with readtable, since writetable doesn't support nested table variables (as I just now discovered). It will only preserve the formatting if you save them as .mat files, and if you're only using them with MATLAB, that shouldn't be a problem.
To illustrate --
Int_force = randn(6,6,28);
Force = repmat(["Axial Force";"Shear Force";"Bending Moment"],2,1);
for ii = 1:28
storey = array2table(Int_force(:,:,ii), VariableNames=["Col "+(1:6)]);
T{:,ii} = table(Force,storey, VariableNames=["Force","Storey "+ii]);
end
T{1:3}
ans = 6×2 table
Force Storey 1 ________________ __________________________________________________________________ Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 _______ ________ ________ ________ _______ ________ "Axial Force" 0.78231 0.53105 1.4562 -0.81678 -1.4183 -0.11741 "Shear Force" 1.4026 -0.44026 0.33258 0.61565 0.64699 0.3443 "Bending Moment" 0.56284 -0.39834 1.0039 0.82518 0.80614 0.88717 "Axial Force" 0.30895 0.021412 0.56862 -0.55521 0.78436 -0.48275 "Shear Force" 0.25915 1.3689 1.6553 1.1399 0.29629 -0.83416 "Bending Moment" -1.8538 -1.7347 -0.34307 -0.36738 0.59558 1.0321
ans = 6×2 table
Force Storey 2 ________________ _____________________________________________________________________ Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 _________ _______ ________ ________ _________ ________ "Axial Force" 1.2662 1.1513 1.677 0.16354 1.6287 0.061989 "Shear Force" -0.031136 0.25733 1.8416 -0.49166 -0.19159 0.90918 "Bending Moment" -2.6795 1.1236 -1.2354 -0.25938 -0.72136 -0.31762 "Axial Force" 0.65128 1.0522 0.018312 -0.23968 0.49121 -2.3822 "Shear Force" -1.5953 -1.3326 -0.36857 0.11955 1.3529 0.44881 "Bending Moment" 0.17701 0.31041 -1.211 0.58318 -0.019887 -0.20031
ans = 6×2 table
Force Storey 3 ________________ ___________________________________________________________________ Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 ________ ________ ________ ________ _______ ________ "Axial Force" -0.92295 0.6145 0.84228 -2.167 0.15108 0.10543 "Shear Force" 0.028968 0.69172 -0.1467 -0.17668 1.5614 -0.56661 "Bending Moment" -1.1062 0.70026 -0.15162 0.33418 0.65028 -0.6649 "Axial Force" -0.26591 0.32916 2.237 0.35838 -1.3169 0.31952 "Shear Force" -0.69389 -0.40468 -2.2357 -0.59301 -0.9845 0.78173 "Bending Moment" -2.1119 1.7749 -1.0331 0.24974 0.89943 0.80198
save('StoreyTables.mat', 'T')
stories = load('StoreyTables.mat')
stories = struct with fields:
T: {1×28 cell}
stories.T{1}
ans = 6×2 table
Force Storey 1 ________________ __________________________________________________________________ Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 _______ ________ ________ ________ _______ ________ "Axial Force" 0.78231 0.53105 1.4562 -0.81678 -1.4183 -0.11741 "Shear Force" 1.4026 -0.44026 0.33258 0.61565 0.64699 0.3443 "Bending Moment" 0.56284 -0.39834 1.0039 0.82518 0.80614 0.88717 "Axial Force" 0.30895 0.021412 0.56862 -0.55521 0.78436 -0.48275 "Shear Force" 0.25915 1.3689 1.6553 1.1399 0.29629 -0.83416 "Bending Moment" -1.8538 -1.7347 -0.34307 -0.36738 0.59558 1.0321
writetable(T{1}, 'Stories1.csv')
Error using writetable (line 278)
Writing nested tables/timetables is not supported. Use splitvars on the nested table before writing.
S1 = readtable('Stories1.csv')
So it would be necessary to creaite them initially as "Storey 1, Col 1" and such if you want to save them using writetable.
.
Thank you once again, Star Strider
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!