MATLAB Answers

Add new line in middle of line of a text file

2 views (last 30 days)
Gifari Zulkarnaen
Gifari Zulkarnaen on 23 Apr 2020
I have a bug in my text file. There should be new line at this blue line:
I have tried to code to fix this:
subdir = 'TCLFiles';
filename = 'Sections.tcl';
str = deblank(fileread(fullfile(pwd,subdir,filename)));
[oldhash,nonhash] = regexp(str,'\#','match','split');
nonhash = nonhash(2:end);
newtext = cell(1,length(oldhash));
for i=1:length(oldhash)
newtext{i} = sprintf('\n%s%s',oldhash{i},nonhash{i});
fid = fopen(fullfile(pwd,subdir,'Sections_fixed.tcl'),'wt');
It works, but it creates many other unnecessary new lines. The result is quite a mess:
Its hard to understand these working-with-text. Any suggestion how to do it properly?
I attach the text file also if you need to take a look. Thank you.


Sign in to comment.

Answers (0)