フィルターのクリア

Saving multi excel sheets to one in MATALB

6 ビュー (過去 30 日間)
Abo
Abo 2016 年 7 月 11 日
コメント済み: Methil Muley 2020 年 7 月 14 日
Hello all,
I have an excel file with 4 sheets: A, B, C, D and each sheet has it own data, i'd like to mix all data in just one sheet and write the value in front of each sheet's name, for example see the photo: thanks
  3 件のコメント
Abo
Abo 2016 年 7 月 15 日
thank you for your suggestion, tried to use following code, but just showing the first sheet values!!
files=dir('*.xlsx');
[~, sheets] = xlsfinfo('Result.xlsx');
for i = 1:numel(sheets)
new = xlsread('Result.xlsx')
T = table(sheets,new)
% writetable(T,'Mergedresult.xlsx')
end
Guillaume
Guillaume 2016 年 7 月 15 日
編集済み: Guillaume 2016 年 7 月 15 日
@Abo, well, inside your loop you don't specify the sheet to read from, so it's always reading from the first one.
@Karen, according to the example, each sheet would only have one row of data but a different number of columns. As tables, you wouldn't be able to concatenate them.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 7 月 15 日
for i = 1:numel(sheets)
T{i} = xlsread('Result.xlsx', sheets{i});
end
Now each T{i} is the data from one sheet, with sheet name sheets{i} . You can manipulate this to get a common format. The best way to do that is going to depend on what your input sheets look like.
You would be wanting to figure out the widest that you need, and create a cell array that wide, in which the cells started out empty. Then fill rows of that with as much data as exists for one sheet, leaving the extra columns empty. The end result would be a cell array you could xlswrite()
  1 件のコメント
Abo
Abo 2016 年 7 月 15 日
編集済み: Abo 2016 年 7 月 15 日
thank you Walter, the out put of my code is like:
the value of sheet 'a' is: 0 191.931 'b': 0 493.4196 ... i'd like to sort it out in column rather than row, i meant the value for sheet 'a' under of the sheet's name... that is the method i developed;
files=dir('*.xlsx');
[~, sheets] = xlsfinfo('Result.xlsx');
for i = 1:numel(sheets)
new{i} = xlsread('Result.xlsx', sheets{i});
T = table(sheets,new);
writetable(T,'Mergedresult.xlsx') ;
end

サインインしてコメントする。


Guillaume
Guillaume 2016 年 7 月 15 日
編集済み: Guillaume 2016 年 7 月 18 日
files=dir('*.xlsx');
[~, sheets] = xlsfinfo('Result.xlsx');
sheetcontent = cellfun(@(sh) xlsread('Result.xlsx', sh), sheets, 'UniformOutput', false); %collect content of each sheet
%before concatenating the contents into one cell array, it needs to be the same size for all sheet
maxcols = max(cellfun(@numel, sheetcontent)); %maximum number of columns
sheetcontent = cellfun(@(c) [num2cell(c), cell(1, maxcols-numel(c))], sheetcontent, 'UniformOutput', false); %append empty cells
sheetcontent = vertcat(sheetcontent{:}); %and concatenate
xlswrite('Mergedresult.xlsx', [sheets(:), sheetcontent]);
edit 18/7/2015: bug
  3 件のコメント
Guillaume
Guillaume 2016 年 7 月 18 日
I made a mistake in the code that appends empty cells (to make the content of each sheet the same width). Fixed now.
Note that the above code assumes that xlsread only read ONE row per sheet.
Methil Muley
Methil Muley 2020 年 7 月 14 日
this give only for number not for characters

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by