Clear Filters
Clear Filters

Need help for export table to excel

2 views (last 30 days)
BN
BN on 13 Jan 2020
Commented: Star Strider on 13 Jan 2020
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

Accepted Answer

Star Strider
Star Strider on 13 Jan 2020
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 Comments
BN
BN on 13 Jan 2020
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 on 13 Jan 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables 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!