How to write cell array to excel file?
27 views (last 30 days)
Show older comments
Abdullah Türk
on 17 Sep 2023
Commented: Dyuman Joshi
on 19 Sep 2023
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 Comments
Accepted Answer
Dyuman Joshi
on 17 Sep 2023
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 Comments
Dyuman Joshi
on 19 Sep 2023
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?
More Answers (2)
Walter Roberson
on 17 Sep 2023
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
on 17 Sep 2023
% 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]);
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!