Matlab's writetable() function only exporting partial data of a table
조회 수: 9 (최근 30일)
이전 댓글 표시
The writetable() function at the end of my code only exports the first row (namely FR_1w, FR_2w and FR_3w),
whereas I want the entire table to be exported and written as .xls or .xlsx.
V=[{A B C};...
{A1 B1 C1};...
{A2 B2 C2}];
X=cell2table(V);
X.Properties.VariableNames= {'FR_1w' 'FR_2w' 'FR_3w'};
X.Properties.RowNames= {'4Weeks' '12Weeks' '24Weeks'};
writetable(X, 'X.xlsx')
n.b. Variables in table V are 3x1 cells.
댓글 수: 2
Walter Roberson
2017년 2월 3일
My understand is that xls and xlsx format do not support multiple values per cell.
You might suggest that multiple consecutive cells should be used to store the values, but it is not clear what should be done for the non-cell portions, and not clear what should be done if the cells are different sizes between the variables in one row, or different sizes between the different rows. Even if all of the variables in a row are the same size and all of the rows use that same size, then should the row names be replicated, or should the row name be put only on the first of them?
채택된 답변
Walter Roberson
2017년 2월 4일
"Tips:
For variables with a cell data type, writetable outputs the contents of each cell as a single row, in multiple fields. If the contents are other than numeric, logical, character, or categorical, then writetable outputs a single empty field."
For text file output, that agrees with my experiments:
V = [{[10;11] [20;21] [30;31]};{[40;41] [50;51] [60;61]};{[70;71] [80;81] [90;91]}];
X=cell2table(V);
X.Properties.VariableNames= {'FR_1w' 'FR_2w' 'FR_3w'};
X.Properties.RowNames= {'4Weeks' '12Weeks' '24Weeks'};
writetable(X,'/tmp/testwt.txt')
>> !cat /tmp/testwt.txt
FR_1w_1,FR_1w_2,FR_2w_1,FR_2w_2,FR_3w_1,FR_3w_2
10,11,20,21,30,31
40,41,50,51,60,61
70,71,80,81,90,91
Notice the new columns.
And when I write to .xlsx and readtable() it back in,
>> writetable(X,'/tmp/testwt.xlsx')
>> readtable('/tmp/testwt.xlsx')
ans =
3×6 table array
FR_1w_1 FR_1w_2 FR_2w_1 FR_2w_2 FR_3w_1 FR_3w_2
_______ _______ _______ _______ _______ _______
10 11 20 21 30 31
40 41 50 51 60 61
70 71 80 81 90 91
Which MATLAB version are you using?
댓글 수: 2
Walter Roberson
2017년 2월 5일
That was the version I tested with, so the answer is that the multiple rows inside the cell get turned into extra columns.
추가 답변 (1개)
Peter Perkins
2017년 2월 6일
John, if Walter's example is indeed what you have, then you are trying to export something that is hierarchical to a file format that is not hierarchical. As Walter describes, writetable does its best to do that. It sounds like you're expecting those cells that contain matrices with multiple rows to be written out as multiple rows in the spreadsheet file. In the long run, I'm skeptical that will be helpful, because it will break the simple correspondence between rows in the table and rows in the file.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!