difficulty in printing for 'cell' inputs.

I have a text file having sentences.I want to edit and print each sentence in a new line .I successfully edited but find difficulty in printing
fid1=fopen('edited_sentences_program3.txt','r');
fid2=fopen('edited_sentences_program4','w');
while ~ feof(fid1)
new_line = fgetl(fid1);
a=new_line;
b=strsplit(a);
c=[b(end) b];
c(end)=[];
fprintf(fid2,'%s\n',c)
end
fclose(fid1);
fclose(fid2);
when I run the code ;error is
"Function is not defined for 'cell' inputs." for line fprintf(fid2,'%s\n',c)

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Feb. 2018

2 Stimmen

fprintf(fid2,'%s\n',c{:})

3 Kommentare

muaz shaikh
muaz shaikh am 22 Feb. 2018
Bearbeitet: Walter Roberson am 22 Feb. 2018
This is sentence
Why yell or worry over silly items?*/sx8
and required sentence in format like
*/sx8 Why yell or worry over silly items?
fprintf(fid2,'%s\n', strjoin(c))
muaz shaikh
muaz shaikh am 22 Feb. 2018
Thanks a lot Roberson it solved my problem

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Jan
Jan am 21 Feb. 2018

1 Stimme

while ~ feof(fid1)
a = fgetl(fid1);
b = strsplit(a);
c = [b(end) b(1:end-1)];
fprintf(fid2, '%s\n', c{:})
end
I've simplified the code. To print the elements of a cell, use c{:}, which converts the cell to a comma separated list of its elements.
The description is "print each sentence in a new line". You code prints each word in a new line and moves the last word before the others. Is this intended? If not, please post an example for the input and the wanted output.

3 Kommentare

muaz shaikh
muaz shaikh am 22 Feb. 2018
This is sentence Why yell or worry over silly items? */sx8 and required sentence in format like */sx8 Why yell or worry over silly items?
Jan
Jan am 22 Feb. 2018
Bearbeitet: Jan am 22 Feb. 2018
@muaz: I have no idea about what this comment means. Nobody is yelling or worrying here and which is the silly item?
You've written "I successfully edited", but your code does not split the text to sentences, but words. Therefore your posted code is confusing. "*/sx8" is a strange format for a sentence. Can you clarify this?
Jan
Jan am 23 Feb. 2018
Oh dear! "Why yell or worry over silly items? */sx8" was an example sentence. I did not get this.

Melden Sie sich an, um zu kommentieren.

Unai San Miguel
Unai San Miguel am 21 Feb. 2018

0 Stimmen

If you are OK with the contents and format of c maybe you can substitute the fprintf line by:
cellfun(@(x) fprintf(fid2, '%s\n', x), c)
This line will go element by element of the cell c and fprint it.

1 Kommentar

muaz shaikh
muaz shaikh am 22 Feb. 2018
thanks mingue,Simon and Roberson;all three answers are given print with every word in a new line where as I want one sentence in one line.I have also tried many other methods but still not succeded...

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 22 Feb. 2018
Bearbeitet: Jan am 22 Feb. 2018

0 Stimmen

Maybe you want this:
Str = fileread('edited_sentences_program3.txt');
Str(ismember(Str, char([10, 13]))) = []; % Remove line breaks.
CStr = strsplit(Str, '.');
fid = fopen('edited_sentences_program4', 'w');
fprintf(fid, '%s.\n', CStr{:});
fclose(fid);
Or even easier:
Str = fileread('edited_sentences_program3.txt');
Str(ismember(Str, char([10, 13]))) = []; % Remove line breaks
Str = strrep(Str, '.', ['.', char(10)]); % Insert new line breaks after each dot
fid = fopen('edited_sentences_program4', 'w');
fwrite(fid, Str, 'char');
fclose(fid);
Maybe even "?" and "!" are valid ends of a sentence. What about "..."? Where does a sentence end, if it contains direct speech?
She said, "I will be at home." and I said, "Me too!"
So please post a relevant input and what you want as output. This would make it much easier to help you to solve your problem.

2 Kommentare

muaz shaikh
muaz shaikh am 22 Feb. 2018
thanks Simon problem is already solved..you also give a good option
Jan
Jan am 22 Feb. 2018
If a given suggestion helped you to solve the problem, please accept it such that the readers care for other questions, which are still open. If no answer helped you, it would be kind if you post your own solution and accept it. This would be useful for the forum.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by