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

조회 수: 51 (최근 30일)
z8080
z8080 2019년 5월 13일
댓글: Peter Perkins 2019년 5월 14일
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?

채택된 답변

Adam Danz
Adam Danz 2019년 5월 13일
편집: Adam Danz 2019년 5월 13일
Change non-cells to cells and then convert.
M.rep = num2cell(M.rep);
M.corrAns = num2cell(M.corrAns);
table2array(M)

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 5월 13일
I recommend using writetable instead of using xlswrite. You can tell writetable to write to a spreadsheet file.
  댓글 수: 1
Peter Perkins
Peter Perkins 2019년 5월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by