write vectors in cell array to excel

조회 수: 18(최근 30일)
Rick Verberne
Rick Verberne 2018년 8월 8일
댓글: Rick Verberne 2018년 8월 9일
Hi all,
I'm wondering if there is any easy way to write vectors stored in a cell array (Data = cell(50,2)) to excel. At the moment I'm using a for loop but it seems quite messy. Preferably I would store them in pairs:
Column A = Data{1,1}
Column B = Data{1,2}
Column C = Data{2,1}
Column D = Data{2,2}
  댓글 수: 3
Rick Verberne
Rick Verberne 2018년 8월 8일
Thank you both for the comments.
No the vectors are not. The two in each row are, but every next row is half the size. The 50 rows represent the same single dataset, but with different bin sizes. In the end I do a curve fitting on these sets and bit more, to find the ratio between peaks in the dataset

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

채택된 답변

OCDER 2018년 8월 8일
Is this what you're trying to do?
%Making a sample cell array with variable-length vectors
Data = cell(50, 2);
for j = 1:50
L = randi(10);
Data(j, :) = {rand(1, L) rand(1, L)};
%Saving to excel format
MaxLen = max(cellfun('length', Data(:, 1)));
AllData = cell(MaxLen, numel(Data));
for j = 1:size(Data, 1)
L = numel(Data{j, 1});
AllData(1:L, 2*j-1) = num2cell(Data{j, 1});
AllData(1:L, 2*j) = num2cell(Data{j, 2});
xlswrite('test.xlsx', AllData)
  댓글 수: 1
Rick Verberne
Rick Verberne 2018년 8월 9일
Great that works. Thank you very much!

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

추가 답변(1개)

dpb 2018년 8월 8일
편집: dpb 2018년 8월 8일
Frankly, the easiest way would be to start with a normal array of nan(50,MaxRowLength) and then fill each row with the results and just write the full array.
NaN will be automagically ignored by plot and friends and you can essentially forget about having to dereference the cell arrays.
Otherwise, just loop over rows and write each with a call to xlswrite at the next row. This will be slow as xlswrite has a lot of overhead baggage; there's a FEX submittal that will allow multiple writes without opening/closing the workbook every time like does xlswrite.
I guess the alternative would be to "regularlize" the cell array with empty cells to the max size; then with each cell being a single value xlswrite should work directly as well.
  댓글 수: 1
Rick Verberne
Rick Verberne 2018년 8월 9일
Thank you, for now I'll stick to the loop as in the above answer. But I'll definitely give this a go. Especially since the speed of the process will get important near the end of the project.

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




Community Treasure Hunt

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

Start Hunting!

Translated by