How to write cell array to excel file?
27 次查看(过去 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!