automating many column headings in table

1 view (last 30 days)
I have a 10000x400 matrix, plotphys, I want to turn into a table and add variable names to. I understand that can be done easily with the array2table function. However adding 400 headings is a lot; luckily I want the headings uniform in that they would be labeled
'phys state at tstep 1', 'phys state at tstep 2', ..., 'phys state at tstep 400'
Is there a way I can basically automate the creation of the headings?
I tried this
head = join("phys state at tstep " + [1:1:400]);
phystable = array2table(plotphys, 'VariableNames', {head});
but that did not work
  2 Comments
Stephen23
Stephen23 on 30 Jan 2025
Edited: Stephen23 on 30 Jan 2025
"but that did not work"
Why did you place the string vector inside a scalar cell array?
H = "phys state at tstep "+(1:4);
T = array2table(rand(3,4), 'VariableNames',H)
T = 3x4 table
phys state at tstep 1 phys state at tstep 2 phys state at tstep 3 phys state at tstep 4 _____________________ _____________________ _____________________ _____________________ 0.76686 0.75948 0.54232 0.41293 0.90409 0.21268 0.53549 0.085162 0.6571 0.58643 0.12335 0.27153
Kitt
Kitt on 30 Jan 2025
I realized soon after posting that mistake.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 30 Jan 2025
Perhaps something like this —
A = randn(5,10);
T1 = array2table(A, VariableNames=compose('physical state at step %4d',1:size(A,2)))
T1 = 5x10 table
physical state at step 1 physical state at step 2 physical state at step 3 physical state at step 4 physical state at step 5 physical state at step 6 physical state at step 7 physical state at step 8 physical state at step 9 physical state at step 10 ___________________________ ___________________________ ___________________________ ___________________________ ___________________________ ___________________________ ___________________________ ___________________________ ___________________________ ___________________________ 0.31324 -0.19263 -2.0615 1.232 1.6304 0.19888 -0.32731 -0.32365 1.5333 0.053857 -2.0234 0.3876 -0.24662 0.13594 -0.9818 0.42247 0.74257 1.2531 0.61061 0.41824 -0.18386 -0.80418 -0.20084 -0.13171 0.9856 -0.21378 -0.45269 0.65468 0.92563 0.2902 -0.11079 -0.91047 -1.0124 -1.2269 -1.5752 -1.095 1.2228 0.19872 -0.14992 -0.43974 0.33118 0.11423 -0.59569 -1.6067 0.71875 0.81034 -0.16905 -0.55092 1.6094 -0.43537
.
  2 Comments
Walter Roberson
Walter Roberson on 30 Jan 2025
Edited: Walter Roberson on 30 Jan 2025
The above uses column names 'physical state at step' followed by a 4 digit number that is right-justified with proceeding spaces -- which is certainly a valid option.
However, if you would prefer to use numbers that have no leading or trailing spaces, then
A = randn(5,10);
T1 = array2table(A, VariableNames="physical state at step "+(1:size(A,2)))
T1 = 5x10 table
physical state at step 1 physical state at step 2 physical state at step 3 physical state at step 4 physical state at step 5 physical state at step 6 physical state at step 7 physical state at step 8 physical state at step 9 physical state at step 10 ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ _________________________ 0.41494 0.89537 0.88637 0.37133 1.5025 -0.23928 0.2796 -0.44267 1.8121 -0.69052 0.62765 -0.29731 -0.76789 0.91972 -0.33827 1.2079 0.75478 0.45772 -0.56342 0.60929 1.6159 0.27437 -1.7607 -0.1301 0.91395 -0.44986 -0.50988 1.0761 -2.3117 -1.326 -1.2961 -1.055 -0.42641 0.51182 -0.45815 -1.1123 -0.54415 1.3361 0.11306 0.82966 -0.75849 -0.42926 0.011669 -0.72911 0.54695 -0.47057 -0.12814 -0.25768 0.59814 1.5258
Kitt
Kitt on 30 Jan 2025
that worked perfectly!!!! Thank you so much :)

Sign in to comment.

More Answers (0)

Categories

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