Clear Filters
Clear Filters

outputfile name using the inputfile name

14 views (last 30 days)
I am trying to have the input file name pulled into the output file name in an excel format, and I added a letter so they can exist in the same folder. However, it is taking the file names with similar starting numbers and combining them even thought the rest of the file name is different. So if there are three files that start with 2022 I only get one excel file 2022 with the last file name in the chain. What am I missing?
EX:
TKL = rmfield(tkl, 'field_');
TK = structure2table(TKL);
cd = 'C: folderlocation';
inputfile = filename(kk).name;
[~,fname] = fileparts(inputfile);
outputfile = sprintf('%s_D.xlsx',fname);
writetable(TK,outputfile,'sheet',1);

Accepted Answer

Image Analyst
Image Analyst on 18 Oct 2022
You need to have a loop over all the files
TKL = rmfield(tkl, 'field_');
TK = structure2table(TKL);
folder = 'C:\folderlocation';
filePattern = fullfile(folder, '/2022*.xlsx');
fileList = dir(filePattern);
numFiles = numel(fileList);
fprintf('Found %d files matching the pattern "%s".\n', numFiles, filePattern)
% Copy TK into the same number of new files having the same name
% but with a "_D" suffix.
for k = 1 : numFiles
fullInputFileName = fullfile(fileList(k).folder, fileList(k).name);
% Prepare the output file name.
[~, baseFileNameNoExt, ext] = fileparts(fullInputFileName);
baseOutputFileName = sprintf('%s_D.xlsx', baseFileNameNoExt);
fullOutputFileName = fullfile(folder, baseOutputFileName);
writetable(TK, fullOutputFileName, 'Sheet', 1);
end

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!