printing full stop in next line

2 ビュー (過去 30 日間)
libra
libra 2018 年 3 月 1 日
コメント済み: Jan 2018 年 3 月 2 日
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.

回答 (1 件)

Jan
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 件のコメント
libra
libra 2018 年 3 月 2 日
編集済み: per isakson 2018 年 3 月 2 日
completed myself by simple programming
b=strsplit(a);c=[b(end) b];
c=cat(2,b(end),b);
c(end)=[];
c=c(1:end);
k=c(end);
l=char(c(end));
l(end)=[];
l=l
d=vertcat(c','.')
g=d(end-1);
h=char(g);
h(end)=[];
h=h;
s=d(end-1);
j=strrep(d,s,h)
fprintf(fid2, '%s\n', j{:})
by changing in above code
Jan
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);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeScripts についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by