when combining excel data how to have it replicate the sheets organization as well

I followed this example that i found on here, i need multiple excel to be all combine in one but I have data on multiple sheets
how do i modify it to tranfer all sheets thank you
fileDir = 'C:\test folder';
outfile = 'C:\test folder\MASTER.xls';
addpath(fileDir);
fileNames = dir(fileDir);
fileNames = {fileNames.name};
fileNames = fileNames(cellfun(...
@(f)~isempty(strfind(f,'.xls')),fileNames));
for f = 1:numel(fileNames),
[~, ~, raw] = xlsread( fullfile(fileDir, fileNames{f}));
xlswrite(outfile, raw, fileNames{f});
end

4 Comments

This code i currently have it creating one excel file per data file
what may i modify to have it save on the same excel file one batch after the other
for file_counter = 1:number_of_files(1)
load(mat_files(file_counter).name)
sn = strsplit(mat_files(file_counter).name,'_');
lgn(file_counter) = sn(3);
title =[data_location lgn{file_counter} '_raw_data.xlsx'];
S21_dB = sparams.post_process_S21_dB(:,1:16);
S41_dB = sparams.post_process_S41_dB(:,1:16);
Nfig = NFig.post_process_noise_figure_dB;
writematrix(S21_dB,title)
writematrix(S41_dB,title, 'Sheet',2)
writematrix(Nfig,title, 'Sheet',3)
Read up on the new(er) functions for handling spreadsheets and ditch the deprecated xlsread/xlswrite combo. See readtable and writetable as well as the supporting functions you'll find on those doc pages at the link for "Functions" at the top. In particular for your above Q?, see sheetnames that will return for you the sheet names of the content of each workbook.
NB: that you can then use a sheet name in both reading and writing; it's not clear what the form of the input sheets/workbooks is nor precisely what you want in the output, but there's also an new ability to append new data onto an existing workbook sheet and you can choose whether to write the header line or not, etc., etc., ...
To write explicit code would mean knowing much more about what you have to start with and what you want, but reading the above doc and studying the examples there should let you do anything you wish...
sorry i am still lost.. I have data in a loop and want to write to the same excel file
Well, so are we without the supporting information/description of the actual form of the input and what it is you're really trying to accomplish.
If you don't want to change workbook file names, then don't create new filenames inside the loop; of course, then you need to know you're writing what you want to what workbook.
If the idea is to do something with multiple sheets in an input file, then as suggested earlier, use sheetnames to return the list of sheets by name in the workbook you opened and then iterate over that array of sheets with the 'Sheet',sheetname named parameter-value pair, reading each in turn and doing what it is that is wanted with each inside that loop. I also pointed out there is the 'Append' named parameter which is either True/False that lets you append new data onto the given sheet (True) rather than overwriting the entire sheet (False).
But, we're just guessing at what it is you're really trying to do and what the input data files really look like...

Sign in to comment.

Answers (0)

Categories

Asked:

on 5 Jan 2023

Commented:

dpb
on 10 Jan 2023

Community Treasure Hunt

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

Start Hunting!