フィルターのクリア

Convert cell array to excel - problem with nested cells

6 ビュー (過去 30 日間)
Melissa Jones
Melissa Jones 2021 年 10 月 17 日
コメント済み: dpb 2021 年 10 月 18 日
Hello!
I really need to write this data from a cell array into an excel file (you can find it attached or in the printscreen). I've used
xlswrite('data.xls', M2)
and
writecell(M2, 'data.xls')
and nothing works because I have nested cells and all with different sizes, some even empty. How can this work?
I need data to be separated like this in the excel:
line1 = M{1,1}
line 2 = M{2,1}
line 3 = M{3,1}
line 4 = M{1,2}
line 5 = M{2,2}
line 6 = M{3,2}
...and so on...
Please help!
  2 件のコメント
dpb
dpb 2021 年 10 月 17 日
You'll have to unnest them first.
Probably an explicit loop or loops will be needed.
BUT--
You can NOT put an array into a single cell in Excel if that is what you're trying to do.
Melissa Jones
Melissa Jones 2021 年 10 月 17 日
Could you help out with those loops then, please?

サインインしてコメントする。

採用された回答

dpb
dpb 2021 年 10 月 17 日
編集済み: dpb 2021 年 10 月 18 日
Try something like
l=0;
for i=1:size(M2,1)
for j=1:size(M2,2)
l=l+1;
writecell(M2(j,i),'M2.xls','Range',"A"+l);
end
end
  7 件のコメント
dpb
dpb 2021 年 10 月 18 日
Well, you've never posted an error or the exact code you tried, so how are we to know what happened?
As noted above, it works w/ R2020b; I can't think what is in the above that has changed terribly recently, but it does use the new(ish) string class overloaded + operator, but that's been around for a while by now.
The above makes it look like that perhaps is the issue; if so replace
...,'Range',"A"+l)
with
,'Range',sprintf('A%d', l));
writecell was introduced in R2019a; if your version predates that then use
writetable(cell2table(M2(j,i)),'M2.xls','Range',sprintf('A%d', l));
as all writecell is is a wrapper around writetable
I will note that if the size of the cell array gets to be large, the above will likely become exceedingly slow and may, even, fail to complete owing to the opening/closing of the Excel file on every cell write operation -- I have run into this on some complex spreadsheets I've been working on for a local nonprofit that need to update disparate locations within the workbook for a few hundred locations.
In that case, the solution will be to build the composite cell array in memory first instead of writing each cell, then make one call to write after have done.
You might also consider how you're constructing the cell array to begin with and not create the nested version at all but build it linearly instead.
dpb
dpb 2021 年 10 月 18 日
" the solution will be to build the composite cell array in memory first..."
Alternatively, and actually the one I implemented above is to use the FEX submission from Nick Oatley @
which will create a persistent COM object at the beginning of the sequence of writes you want to make and then let you close it when done.
This will make probably an order of magnitude difference in speed and is also not prone to the issues I outlined above if the size of M gets large.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 10 月 17 日
You have several options:
  • write each cell entry as text, perhaps using M2s = cellfun(@mat2str, M2, 'uniform', 0) . If you do that, then Excel will not be able to interpret the results as numeric
  • Provided that you do not have any natural "nan" entries, convert each entry into a column and append a nan to it, and combine the entries. You might need to pad each column to the same length using nan. So the first column would contain 52 double, then a nan, then 34 double, then a nan, then 6 nan (longest column would be 92 entries)
  • find the widest column and pad each of them out to that size. Then rearrange the entries into continuous columns. Your widest is 74 columns, so you would pad each of the 1 x N out to be 1 x 72, using NaN as your padding; then you would rearrange to a cell containing a letter, then 72 cells with numeric values, then another 72 cells of numeric values, and each column would have that length.
  • expand the first column into 52 columns (of 3 rows), expand the second column into 74 columns, the third into 60 columns, and so on, with the number of new colums needed being equal to the widest row in that column. You might need to put in a new row that indicates which original column each entry belonged to.
  1 件のコメント
Melissa Jones
Melissa Jones 2021 年 10 月 17 日
The first way seemed the simplest.
But Isnt there a way to put each number in a separate cell in excel, disposed as I mention in the original question?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by