printing full stop in next line
2 次查看(过去 30 天)
显示 更早的评论
fid1=fopen('prompts2','r');
fid2=fopen('prompts3trialtimitsmall','w');
while ~ feof(fid1)
new_line = fgetl(fid1);
a=new_line;
b=strsplit(a);
c=[b(end) b];
c(end)=[];
c=c;
fprintf(fid2, '%s\n', c {:})
end
fclose(fid1);
fclose(fid2);
my sentences in prompts2 are like this Alfalfa is healthy for you. */sx21 sentences in output file is
*/sx21
Alfalfa
is
healthy
for
you.
I want
*/sx21
Alfalfa
is
healthy
for
you
.
i.e. last full stop to print in next line I have tried converting z= char(c(end)) and then printing but not succed.
0 个评论
回答(1 个)
Jan
2018-3-1
编辑:Jan
2018-3-2
Replace:
fprintf(fid2, '%s\n', c {:})
by
str = sprintf('%s\n', c {:});
str = strrep(str, '.', [char(10), '.']); % [EDITED, typo fixed]
fprintf(fid2, '%s', str);
3 个评论
Jan
2018-3-2
also I have tried many different options with strrep but not worked
Yes, I had a typo in my code. Replace
str = strrep('.', [char(10), '.']);
by
str = strrep(str, '.', [char(10), '.']);
Using the curly braces to access a cell element is more efficient than creating a scalar cell at first and converting it by char:
% l=char(c(end))
l = c{end}; % Much better and nicer
This is not meaningful:
h=h;
% or
c=c;
This is simply a waste of time and confuses the readers. This is cluttering also:
new_line = fgetl(fid1);
a=new_line;
What about:
a = fgetl(fid1)
?
This does the same thing twice, so omit one of the lines:
c=[b(end) b];
c=cat(2,b(end),b);
This is not used anywhere, to better remove it:
k=c(end);
Finally let me summary my suggestion with your original (much clearer) code:
fid1 = fopen('prompts2','r');
fid2 = fopen('prompts3trialtimitsmall','w');
while ~ feof(fid1)
a = fgetl(fid1);
b = strsplit(a);
c = [b(end), b(1:end-1)];
str = sprintf('%s\n', c{:});
str = strrep(str, '.', [char(10), '.']);
fwrite(fid2, str, 'char'); % Faster than FPRINTF
end
fclose(fid1);
fclose(fid2);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!