필터 지우기
필터 지우기

outputfile name using the inputfile name

조회 수: 14 (최근 30일)
WENDILYN GUIDOTTI
WENDILYN GUIDOTTI 2022년 10월 18일
댓글: WENDILYN GUIDOTTI 2022년 10월 18일
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
  댓글 수: 1
WENDILYN GUIDOTTI
WENDILYN GUIDOTTI 2022년 10월 18일
This worked so great, I appreciate you!

댓글을 달려면 로그인하십시오.

추가 답변 (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