Need help for export table to excel

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 日

1 投票

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 日
編集済み: BN 2020 年 1 月 13 日
Thank you Star Strider
All is worked very well except the name of xlsx files. after using your above mention code all excel file saved in this format:
test001.xlsx
test002.xlsx
test003.xlsx
...
test125.xlsx
it's okay I can change the names manually although it takes a lot of time. I was wondering if there is any way to automatically write the first good value (because of a probably empty cell in the first) from the second column of each table for every excel file or not.
Thank you again
Star Strider
Star Strider 2020 年 1 月 13 日
My pleasure.
Changing the file names here is very easy to do.
You can skip over any elements of ‘C’ that are empty.
I do not understand what you want to name them, or what you want to write to them.
BN
BN 2020 年 1 月 13 日
Thanks. I want to name each xlsx file based on the second column of each one. for example in one table, if there is the table:
Capture.JPG
I want to save it with the name of Abadan.xlsx
in fact, every 125 table has its own station_name and I want to save them using this kind of name.
another remarkable point is sometimes the station_name column is empty in some cell but I'm sure at least one cell in this column represents station_name and I want to use it.
Star Strider
Star Strider 2020 年 1 月 13 日
As always, my pleasure!
Try this:
C = {array2table(rand(4),'VariableNames',{'AA','BB','CC','DD'}), array2table(rand(3),'VariableNames',{'EE','FF','GG'})} % Create ‘C’
for k = 1:numel(C)
filename = C{k}.Properties.VariableNames(2)
writetable(C{k},sprintf('%s.xlsx',filename{:}))
end
My apologies for the delay. I needed to create my own version of ‘C’ (that I included here to be sure it creates the correct table format) to test this to be certain it does what you want. If I understand correctly, it does.
Consider using fullfile to be certain you are writing them to the directory you want them to go to.
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 件)

質問済み:

BN
2020 年 1 月 13 日

コメント済み:

2020 年 1 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by