How to delete a string in multiple files by using eraseBetween (R2019a)

조회 수: 1 (최근 30일)
Blue
Blue 2019년 5월 22일
Hello,
I am trying without success to delete a specific string (everything between xmln= and the next space) in multiple text files (xml files) by using the function eraseBetween. The files would then be moved to a new folder. The code so far is below. Any help welcome.
modified_xml = fullfile(pwd, 'modified_xml')
if exist([pwd '\modified_xml'])~=7
mkdir(modified_xml);
end
InputOldFiles = dir(fullfile('*.xml'));
OldFilesNames = {InputOldFiles.name};
for k = 1 : length(InputOldFiles)
OldFiles = OldFilesNames{k};
FID = fopen(OldFiles, 'r');
if FID == -1, error('Cannot open file'), end
% Delete specific string (everything between xmlns= and the next empty space)
text = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', '');
new_text = eraseBetween(text,"xmlns="," ");
NewFiles = fopen(OldFiles, 'w');
if NewFiles == -1, error('Cannot open file'), end
% Save the file
fprintf(NewFiles, '%s\n', new_text{:});
fclose(NewFiles);
% Copy the files
copyfile(OldFiles, NewFiles);
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by