MATLAB Answers

Converting a table of mixed numeric-string content to an array

70 views (last 30 days)
z8080
z8080 on 13 May 2019
Commented: Peter Perkins on 14 May 2019
I have a table with mixed numeric&string content:
M =
rep file condition corrAns
___ __________________ _________ _______
1 'stimuli/03C1.wav' 'C1' 2
1 'stimuli/03C4.wav' 'C4' 1
......................
I'd like to convert M to an array, so as to be able to save it as a spreadsheet using xlswrite, which requires an array as input.
However, the command
table2array(M)
gives the error
Cannot concatenate the table variables 'rep' and 'file', because their types are double and cell.
I tried converting the first and final columns from double to cell using
M(:,1) = table2cell(M(:,1))
or even
for i_row=1:row_N
M(i_row,1) = num2cell(M(i_row,1));
M(i_row,4) = num2cell(M(i_row,4));
end
but both of these conversions lead to the same error. Any suggestions?

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 13 May 2019
Edited: Adam Danz on 13 May 2019
Change non-cells to cells and then convert.
M.rep = num2cell(M.rep);
M.corrAns = num2cell(M.corrAns);
table2array(M)

More Answers (1)

Steven Lord
Steven Lord on 13 May 2019
I recommend using writetable instead of using xlswrite. You can tell writetable to write to a spreadsheet file.

  1 Comment

Peter Perkins
Peter Perkins on 14 May 2019
What Steve said. writetable is a much better choice.
It also looks like Condition could usefully be converted to categorical, depending on what you're doing with it.

Sign in to comment.

Sign in to answer this question.