필터 지우기
필터 지우기

Output name same as input names but with an extra word at the end to distinguish

조회 수: 3 (최근 30일)
Hello ,
I have many files in a folder . I have written a matlab code to do something. Now I am looking to save the output file with the same name as the input file but with an extra word at the end to distinguish since they are all going to be in the same folder.
Here is just an idea
Input file = A.txt
Output file = A.edited.txt
Is it possible to do this? I should also mention that the files are going to be in a folder and I want to have a loop where the code would read one file at a time, do what I want it to do and save it as a excel file with the modified input name.
Thanks

채택된 답변

Adam Danz
Adam Danz 2019년 8월 31일
If you're reading in a txt file and saving an excel file, you don't need to have different names but it still might be a good idea to add the "edited" tag so you can keep track of which data you're looking at.
Avoid using dots in filenames since they indicate file extensions. Underscores a better.
inputfile = 'A.txt';
[~,fname] = fileparts(inputfile);
outputfile = sprintf('%s_edited.xlsx',fname);
Result
outputfile =
'A_edited.xlsx'
  댓글 수: 5
Gopika Rajan
Gopika Rajan 2019년 9월 1일
Thanks. It is working great. Could you help me with another related problem? The files are going to be in a folder and I want to have a loop where the code would read one file at a time, do what I want it to do and save it as a txt file with the modified input name(where I would be using the code you suggested.
Adam Danz
Adam Danz 2019년 9월 1일
I'm not sure what the question is. I'm guessing your quesiton is how to list each file and read it in individually.
You can use dir() to list the content of a directory. This line below, for example, reads in lists all txt files in a directory.
% chosenPath is the path to your folder
files = dir(fullfile(chosenPath, '*.txt'));
filenames = {files.name};
Then can loop through those files
for i = 1:numel(filenames)
filenames{i}
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by