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 Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by