How to write cell array to excel file?
조회 수: 19 (최근 30일)
이전 댓글 표시
Hi,
I have a 9x5 cell array matrix. I want to export it as excel format. I presented it named cell matrix.
How can I export this matrix as excel format?
Thanks.
댓글 수: 0
채택된 답변
Dyuman Joshi
2023년 9월 17일
The data you have is stored in a weird manner.
load('cell matrix.mat')
whos
169560 bytes for a 9x5 cell, Hmmm.
final_best_p_worker
Each cell element consists a 1x16 cell.
final_best_p_worker{1}
And then each cell element consists a column vector.
There is one uniformity we can work i.e. the total number of elements for each cell element is same (250), so it is possible to vertically concatenate.
for k=1:numel(final_best_p_worker)
y(k)=sum(cellfun('length',final_best_p_worker{k}));
end
unique(y)
So final product from each cell element will be 250x1 -
%Vertically concatenating data in each cell
for k=1:numel(final_best_p_worker)
final_best_p_worker{k} = vertcat(final_best_p_worker{k}{:});
end
final_best_p_worker
After this, you have 3 options how do you want your final data to be stored -
%Option 1
out1 = cell2mat(final_best_p_worker)
%Option 2
out2 = horzcat(final_best_p_worker{:})
%Option 3
out3 = vertcat(final_best_p_worker{:})
Choose whichever size you want to save your data as and use that variable as input to xlswrite() -
%As you are working with R2015a version, use xlswrite()
%as writematrix() was introduced in R2019a
xlswrite('matrix.xlsx',array_you_want_to_save)
댓글 수: 5
Dyuman Joshi
2023년 9월 19일
I don't understand - How did 12 come here?
You have a 9x5 cell where each element is 1x16 cell. Where did 12 come from?
추가 답변 (2개)
Walter Roberson
2023년 9월 17일
writecell() in later releases. In your release you are either going to need to make a bunch of xlswrite calls or else you are going to need to create an activex connection to excel and use the connection to send data.
Diwakar Diwakar
2023년 9월 17일
% Load your cell array
load('cell_matrix.mat', 'final_best_p_worker');
% Flatten the cell array into a cell array
flattened_data = cellfun(@(x) x(:)', final_best_p_worker, 'UniformOutput', false);
% Convert the flattened cell array to a table
table_data = cell2table(flattened_data);
% Define excel file
excel_file = 'output_data.xlsx';
% Use the writetable function to export the table to an Excel file
writetable(table_data, excel_file);
% Display a message indicating the successful export
disp(['Data has been exported to ' excel_file]);
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
