Need help for export table to excel
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I have a 1*125 cell named "C" which has 125 tables. I want to export all these 125 tables to separated xlsx files (every table has specific xlsx file). I want the name of each xlsx file to be the first good value (because of a probably empty cell in the first) in the second column (station name). I was try to do this:
Behzad = cell2table (C);
writetable(Behzad,'test.xlsx','Sheet',1);
but after do that the texst.xlsx conatin nothing but C1, C2, C3 which I don't know what that means.
Thank you
0 个评论
采纳的回答
Star Strider
2020-1-13
I am not certain what your cell array consists of.
This prototype code (that appears to construct ‘C’ to match your description of it) works correctly for me:
C = {array2table(rand(4)), array2table(randn(3))}; % Create ‘C’
for k = 1:numel(C)
writetable(C{k},sprintf('test%03d.xlsx',k))
end
I verified that the Excel tables were written correctly. (Now, I am going to delete them.)
6 个评论
Star Strider
2020-1-13
As always, my pleasure!
I misunderstood what you wanted.
Try this:
D = load('C.mat');
C = D.C;
for k = 1:numel(C)
filename = C{k}{1,2};
if ~isempty(filename{:})
sprintf('%s.xlsx',filename{:})
writetable(C{k},sprintf('%s.xlsx',filename{:}))
end
end
The ‘filename’ assignment takes the first row element in the second column to use as the file name. The code skips over the tables that are empty in that column, and only write files for those with strings in that column. If I understand correctly, that should do what you want.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!