write vectors in cell array to excel
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rick Verberne
el 8 de Ag. de 2018
Comentada: Rick Verberne
el 9 de Ag. de 2018
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}
etc
Best,
Rick
3 comentarios
dpb
el 8 de Ag. de 2018
If the cell array is as appears of one element per cell, then xlswrite will do it transparently.
If the data are all numeric and the same size as Stephen is preparing to say ( :) ) you can convert the cell to an array and xlswrite will handle that, too.
If the cells aren't regular, there's more work to do and it then depends on that structure as to how but given your output format it doesn't appear to be the issue.
Respuesta aceptada
OCDER
el 8 de Ag. de 2018
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)};
end
%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});
end
xlswrite('test.xlsx', AllData)
Más respuestas (1)
dpb
el 8 de Ag. de 2018
Editada: dpb
el 8 de Ag. de 2018
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.
ADDENDUM
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.
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!