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

采纳的回答

Star Strider
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 个评论
BN
BN 2020-1-13
Thank you for your time and your answer. I'm sorry if I'm didn't explain clear and providing sample data. I was run the code but in the end, just one excel file namely station_name was created.
I want 125 excel files and name them using value inside the station_name column.
in the previous code, all is well except the name of excel files.
I attached C for you. sorry for not attached it before because I think it's so big to attach but I saw that it is just 2 MB.
in the C I have 125 tables, each of them represents data for one station. I want to export them to excel with the name that represents in station_name column of each one. like:
Abadan.xlsx
Abadeh.xlsx
... and so on.
thank you again.
Star Strider
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 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by