working with files, changing in the text

8 views (last 30 days)
Kira Babenko
Kira Babenko on 3 Feb 2020
Commented: Kira Babenko on 3 Feb 2020
Hi
I really was trying to find out how to solve my task, but I can't. I need to read from the text 3 lines and change '7','8','9' to '!'
f=fopen('kik.txt','rt');
while feof(f)==0
s=fgetl(f);
for i=1:length(s)
if s(i)=='8'
s(i)='!';
end
if s(i)=='9'
s(i)='!';
end
if s(i)=='7'
s(i)='!';
end
end
end
disp(s)
fclose(f);

Answers (1)

Bhaskar R
Bhaskar R on 3 Feb 2020
Edited: Bhaskar R on 3 Feb 2020
data = fileread('kik.txt'); % your text file data
rep_data = regexprep(data, '[789]', '!'); % replaced data
fid = fopen('output_file.txt', 'wt'); % open file for writing
fprintf(fid,'%s', rep_data); % writing replaced data to text file
fclose(fid); % Close file
  3 Comments

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!