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

1 Ansicht (letzte 30 Tage)
Blue
Blue am 22 Mai 2019
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

Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by