Horizontally concatenating table columns: combining a table column and a 2d array output by str2double() into one table

2 views (last 30 days)
I'm reading in spreadsheet data from a file and storing that in the variable wab_table = readtable("WAB Summary Statistics Behavioral Data Language Scores.xlsx"); . The first column of this table contains subject ids, and I get the subject ids I need using the variable row_indices which contains the following.
8 14 10 9 2 4 7 3 6 11
There's also a variable i create called all_subjects which just has the typed out version of the subject ids at each of these row indices.
all_subjects =
10×1 string array
"DE-008"
"EX-014"
"FL-010"
"IS-009"
"KI-002"
"KX-004"
"NS-007"
"SH-003"
"SN-006"
"TO-011"
The following columns all have numerical data I'm interested in: [5;15;25;35;45]; . So I try to index into these columns at 10 selected row indices within the variable row_indices
avg_baselines_table_withnans = wab_table(row_indices,[5;15;25;35;45]); % unlabelled
avg_baselines_nonans = [];
for i=1:5
col = str2double(avg_baselines_table_withnans.(i));
avg_baselines_nonans = [avg_baselines_nonans, col];
end
avg_baselines_table_withnans is a subtable created using the indexing into wab_table as I wrote it above. Every column in avg_baselines_table_withnans is supposed to have numerical data but there are certain entries that have strings and study notes/things that can't be converted to a number.
I figured I needed to call str2double() on this subtable. But if I tried str2double(avg_baselines_table_withnans) this would output just one "Nan" for the whole table rather than converting each entry. I ultimately had to settle on the for loop I created above.
So at this point... I want to create a table that has the column for the subject ids first, and then it should have columns for each column in avg_baselines_nonans which should be the array created from passing each column from avg_baselines_table_withnans into str2double() .
Things work the way I think they should if I convert avg_baselines_nonans to a cell array and try to concatenate cell arrays by indexing into wab_table using {} operators and num2cell()
avg_baselines_cell_arr2 = [ wab_table{row_indices, 1}, num2cell(avg_baselines_nonans) ];
avg_baselines_table = array2table(avg_baselines_cell_arr2, 'VariableNames', {'Subj' 'avg_wabaq' 'avg_spont' 'avg_avc' 'avg_rep' 'avg_naming'})
This creates the table avg_baselines_table with columns listed as is in 'VariableNames'. Good.
I have a for loop that goes through a series of lines that use conditional indexing. Here is one of those lines. This works as I expect it to as long as I have concatenated cell arrays
subject_avg_baselines_row = avg_baselines_table(avg_baselines_table.Subj==all_subjects(i),:) % from the for loop
avg_baselines_table(avg_baselines_table.Subj==all_subjects(1),:)
What's confusing me is that these lines don't work as intended if, instead of using cell indexing/cell arrays, I just try to use normal arrays and normal indexing using () operators instead.
If I rewrite avg_baselines_cell_arr2 to the following...
avg_baselines_cell_arr2 = [wab_table(row_indices, 1), array2table(avg_baselines_nonans)];
avg_baselines_table = array2table(avg_baselines_cell_arr2, 'VariableNames', {'Subj' 'avg_wabaq' 'avg_spont' 'avg_avc' 'avg_rep' 'avg_naming'})
Then I get a table that actually seems to have two rows of headers?
array2table([wab_table(row_indices, 1), array2table(avg_baselines_nonans)], 'VariableNames', {'Subj' 'avg_wabaq' 'avg_spont' 'avg_avc' 'avg_rep' 'avg_naming'})
ans =
10×6 table
Subj avg_wabaq avg_spont avg_avc avg_rep avg_naming
Var1 avg_baselines_nonans1 avg_baselines_nonans2 avg_baselines_nonans3 avg_baselines_nonans4 avg_baselines_nonans5
And if I try to proceed with this table, then my conditional statements such as avg_baselines_table(avg_baselines_table.Subj==all_subjects(1),:) don't work.
Instead they have to be changed to avg_baselines_table(avg_baselines_table.Subj.Var1==all_subjects(1),:) i.e. I have to use dot notation to refer to both column names or a dynamic selector like avg_baselines_table.Subj.(1)
So at this point I'm wondering
  • if str2double() outputs a 2d array/matrix, how exactly do I merge/concatenate that array with the table column at wab_table(row_indices, 1) ? Do I have to use cell indexing, or is there a way to use normal indexing and for things to still work?
  • I'm calling array2table twice because if you use the concatenor operators [] then everything within those operators needs to be a table otherwise the concatenation fails/gives an error. So I'm calling array2table around the concatenated table columns of interest and I'm calling array2table within the concatenation to convert the array produced by str2double into a table. It seems like this is what's causing me to have this problem. Is there a way I can concatenate the column containing subject ids with the output of str2double so that I don't have to worry about having two variable headers?

Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!