Clear Filters
Clear Filters

Sorting columns by header names

31 views (last 30 days)
Hi,
I have a table with mess up data location like this.
I want to bring all the same Columns letters together. I am wondering if there is an easy way to do this?
I have try to impliment bubble sort as follow but running into some compare text problem
function [table_out] = sort_text(table_in)
table_size = size(table_in(1,:)); % table size
for i = 1:table_size(2)
for j = 1:table_size(2)
if strcmp(char(table_in.Properties.VariableNames(j)), char(table_in.Properties.VariableNames(j+1))) == 1
table_in = swap(table_in(j),table_in(j+1));
end
end
end
end

Accepted Answer

the cyclist
the cyclist on 7 Sep 2023
% Make up a data table
N = 3;
Column_B_data1 = rand(N,1);
Column_A_data1 = rand(N,1);
Column_C_data2 = rand(N,1);
Column_D_data2 = rand(N,1);
Column_A_data2 = rand(N,1);
Column_B_data2 = rand(N,1);
Column_D_data1 = rand(N,1);
tbl = table(Column_B_data1,Column_A_data1,Column_C_data2,Column_D_data2,Column_A_data2,Column_B_data2,Column_D_data1)
tbl = 3×7 table
Column_B_data1 Column_A_data1 Column_C_data2 Column_D_data2 Column_A_data2 Column_B_data2 Column_D_data1 ______________ ______________ ______________ ______________ ______________ ______________ ______________ 0.017808 0.33524 0.3062 0.85413 0.57774 0.39403 0.70176 0.33161 0.21866 0.8892 0.66124 0.79637 0.24335 0.79331 0.41375 0.58761 0.32401 0.25822 0.038418 0.80929 0.77397
% Find the sorting order
[~,sortingIndex] = sort(tbl.Properties.VariableNames);
% Sort into a new table
new_tbl = tbl(:,sortingIndex)
new_tbl = 3×7 table
Column_A_data1 Column_A_data2 Column_B_data1 Column_B_data2 Column_C_data2 Column_D_data1 Column_D_data2 ______________ ______________ ______________ ______________ ______________ ______________ ______________ 0.33524 0.57774 0.017808 0.39403 0.3062 0.70176 0.85413 0.21866 0.79637 0.33161 0.24335 0.8892 0.79331 0.66124 0.58761 0.038418 0.41375 0.80929 0.32401 0.77397 0.25822

More Answers (1)

Bruno Luong
Bruno Luong on 7 Sep 2023
Edited: Bruno Luong on 7 Sep 2023
A=rand(10,1);
B=rand(10,1);
Z=rand(10,1);
T=table(Z,B,A)
T = 10×3 table
Z B A ________ ________ _______ 0.74455 0.3744 0.84735 0.23491 0.77743 0.16809 0.17916 0.86516 0.61983 0.076788 0.096141 0.54182 0.098915 0.89129 0.72997 0.23829 0.58839 0.73499 0.69243 0.57171 0.38707 0.26114 0.32184 0.6048 0.016699 0.15426 0.38407 0.14351 0.84739 0.57648
[~,is]=sort(T.Properties.VariableNames);
T = T(:,is)
T = 10×3 table
A B Z _______ ________ ________ 0.84735 0.3744 0.74455 0.16809 0.77743 0.23491 0.61983 0.86516 0.17916 0.54182 0.096141 0.076788 0.72997 0.89129 0.098915 0.73499 0.58839 0.23829 0.38707 0.57171 0.69243 0.6048 0.32184 0.26114 0.38407 0.15426 0.016699 0.57648 0.84739 0.14351
  1 Comment
Steven Lord
Steven Lord on 7 Sep 2023
If your table had a mostly-sorted set of variables and you only need to move one or two variables, the movevars function may be of use instead of indexing.
A=rand(10,1);
B=rand(10,1);
Z=rand(10,1);
T=table(A, Z, B)
T = 10×3 table
A Z B ________ ________ _______ 0.74294 0.23125 0.56058 0.25209 0.96923 0.85752 0.92007 0.76815 0.59707 0.23497 0.083857 0.39342 0.31552 0.89183 0.94034 0.92638 0.31848 0.65163 0.35944 0.8189 0.44748 0.59282 0.54002 0.39289 0.027912 0.78539 0.2909 0.37218 0.69359 0.28342
In release R2023a and later, you can move a variable to the end by calling movevars with two inputs. For prior releases you could specify 'After' and tell MATLAB to move the variable after the last variable using the output of width on the table.
T2 = movevars(T, "Z") % Move Z to the end
T2 = 10×3 table
A B Z ________ _______ ________ 0.74294 0.56058 0.23125 0.25209 0.85752 0.96923 0.92007 0.59707 0.76815 0.23497 0.39342 0.083857 0.31552 0.94034 0.89183 0.92638 0.65163 0.31848 0.35944 0.44748 0.8189 0.59282 0.39289 0.54002 0.027912 0.2909 0.78539 0.37218 0.28342 0.69359
T3 = movevars(T, "Z", "After", width(T))
T3 = 10×3 table
A B Z ________ _______ ________ 0.74294 0.56058 0.23125 0.25209 0.85752 0.96923 0.92007 0.59707 0.76815 0.23497 0.39342 0.083857 0.31552 0.94034 0.89183 0.92638 0.65163 0.31848 0.35944 0.44748 0.8189 0.59282 0.39289 0.54002 0.027912 0.2909 0.78539 0.37218 0.28342 0.69359
Or if you're adding table variables incrementally, you could use addvars to add the new variable in a particular location.
C = (1:height(T2)).';
T4 = addvars(T2, C, 'After', "B")
T4 = 10×4 table
A B C Z ________ _______ __ ________ 0.74294 0.56058 1 0.23125 0.25209 0.85752 2 0.96923 0.92007 0.59707 3 0.76815 0.23497 0.39342 4 0.083857 0.31552 0.94034 5 0.89183 0.92638 0.65163 6 0.31848 0.35944 0.44748 7 0.8189 0.59282 0.39289 8 0.54002 0.027912 0.2909 9 0.78539 0.37218 0.28342 10 0.69359

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!