Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).
% Generate Table
it_num_cols = 4;
it_num_rows = 5;
mt_data = rand([it_num_rows, it_num_cols]);
tb_test = array2table(mt_data);
cl_col_names = strcat('col_', string((1:it_num_cols)));
cl_row_names = strcat('row_', string((1:it_num_rows)));
tb_test.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
tb_test.Properties.RowNames = matlab.lang.makeValidName(cl_row_names);
rng(123);
mean = strcat('data=', string(rand([it_num_rows,1])));
sd = strcat('data=', string(rand([it_num_rows,1])));
tb_test_ori = addvars(tb_test, mean, sd);
% Move Variable
tb_test_varmove = movevars(tb_test_ori, {'mean', 'sd'}, 'Before', 'col_1');
% Display
disp(tb_test_ori);
              col_1       col_2      col_3      col_4          mean               sd      
             ________    _______    _______    _______    ______________    ______________
    row_1     0.34318      0.738     0.6344    0.32296    "data=0.69647"    "data=0.42311"
    row_2     0.72905    0.18249    0.84943    0.36179    "data=0.28614"    "data=0.98076"
    row_3     0.43857    0.17545    0.72446    0.22826    "data=0.22685"    "data=0.68483"
    row_4    0.059678    0.53155    0.61102    0.29371    "data=0.55131"    "data=0.48093"
    row_5     0.39804    0.53183    0.72244    0.63098    "data=0.71947"    "data=0.39212"
disp(tb_test_varmove);
                  mean               sd           col_1       col_2      col_3      col_4 
             ______________    ______________    ________    _______    _______    _______
    row_1    "data=0.69647"    "data=0.42311"     0.34318      0.738     0.6344    0.32296
    row_2    "data=0.28614"    "data=0.98076"     0.72905    0.18249    0.84943    0.36179
    row_3    "data=0.22685"    "data=0.68483"     0.43857    0.17545    0.72446    0.22826
    row_4    "data=0.55131"    "data=0.48093"    0.059678    0.53155    0.61102    0.29371
    row_5    "data=0.71947"    "data=0.39212"     0.39804    0.53183    0.72244    0.63098
Rename the first Column, rename the ‘sd’ column, then rename the 3rd and 4th Columns. Note for multiple column renaming, use parenthesis, but for single column renaming, use bracket.
tb_test_varmove.Properties.VariableNames{1} = 'RenameMean';
tb_test_varmove.Properties.VariableNames{'sd'} = 'RenameSDCol';
tb_test_varmove.Properties.VariableNames([3 4]) = {'3rd' '4th'};
disp(tb_test_varmove);
               RenameMean       RenameSDCol        3rd         4th       col_3      col_4 
             ______________    ______________    ________    _______    _______    _______
    row_1    "data=0.69647"    "data=0.42311"     0.34318      0.738     0.6344    0.32296
    row_2    "data=0.28614"    "data=0.98076"     0.72905    0.18249    0.84943    0.36179
    row_3    "data=0.22685"    "data=0.68483"     0.43857    0.17545    0.72446    0.22826
    row_4    "data=0.55131"    "data=0.48093"    0.059678    0.53155    0.61102    0.29371
    row_5    "data=0.71947"    "data=0.39212"     0.39804    0.53183    0.72244    0.63098
Remove Table ColumnRemove columns from the Table
tb_test_varmove_drop = removevars(tb_test_varmove, {'3rd', 'col_3'});
disp(tb_test_varmove_drop);
               RenameMean       RenameSDCol        4th       col_4 
             ______________    ______________    _______    _______
    row_1    "data=0.69647"    "data=0.42311"      0.738    0.32296
    row_2    "data=0.28614"    "data=0.98076"    0.18249    0.36179
    row_3    "data=0.22685"    "data=0.68483"    0.17545    0.22826
    row_4    "data=0.55131"    "data=0.48093"    0.53155    0.29371
    row_5    "data=0.71947"    "data=0.39212"    0.53183    0.63098