working with files, changing in the text
8 views (last 30 days)
Show older comments
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);
0 Comments
Answers (1)
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
See Also
Categories
Find more on Standard File Formats in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!