outputfile name using the inputfile name

14 次查看(过去 30 天)
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);

采纳的回答

Image Analyst
Image Analyst 2022-10-18
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by