MATLAB: How to concatenate table variables with underscores in their name

best practicedynamic headersdynamic table headersevaleviltableunderscorevariables

I have variables like B1_1_3(53×1) B2_1_3(53×1) until B56_1_3(53×1) stored in my table A. I want to concatenate them to receive a new vector(53×56). I tried:
for k in 1:56
a = [A.B{k}_1_3, A.B{k}_1_3];
end
However this does not work. Any suggestions? Thank you in advance!

Best Answer

No loop needed. Just list all of the table headers, pull out the ones that take the form "B#_1_3", and then create a matrix based on values from those columns.
% Create demo table
A = array2table(rand(6,4),'variableNames',{'B1_1_3','B2_1_3','B3_1_3','total'});
% List all headers
headers = A.Properties.VariableNames;
% Extract headers that have the form B#_1_3
goodHeaders = regexp(headers,'B\d+_1_3','Match');
goodHeaders = [goodHeaders{:}]';
% Create matrix of values from good header columns *see Guillaume's comment below
m = cell2mat(cellfun(@(x)A.(x)',goodHeaders,'UniformOutput',false))';