필터 지우기
필터 지우기

Write the lines (sentences) of a 1513 x 1 string into separate lines in a text file, keeping the whole length of each sentence, without breaking them.

조회 수: 3 (최근 30일)
This is the function I am using: writelines(newstr,"c:\users\lnitz\downloads\tarnowagain.txt")
newstr is a 1513 x 1 string, consisting of whole sentences from a story.Many sentences are quite long.
The text below was two sentences (each in a single line) in the string document. but appears broken into four lines in the writelines output.
Sie hatte keine Kinder, und mir wurde daher
der ganze Reichthum ihres Herzens an Liebe.
Seit meinem zehnten Jahre war sie mir Pflegerin
und Erzieherin, und mein Herz liebte und ehrte sie wie eine zweite Mutter.
My ideal is to have either a text file with one sentence per line or a spreadsheet with one sentence in the first cell of each line. This woujld be input to a text program that focuses on sentences.

채택된 답변

Samay Sagar
Samay Sagar 2024년 4월 25일
편집: Samay Sagar 2024년 4월 28일
The issue you are encountering likely stems from the inherent formatting of the “newstr” string. This can happen if the sentences in “newstr” contain newline characters. You can use the “replace” function to remove the newline characters.
To ensure each sentence is recognized and treated as a distinct entity that occupies a single line, you can leverage MATLAB's “regexp” function. You can apply the “regexp” function to your text data, ensuring every sentence is correctly identified.
Here is how you can implement the above solution :
newstr ="This is a sentence. Here's another one! And yet another one?";
newstr = replace(newstr, newline, "");
newstr = replace(newstr, "\n", "");
sentences = regexp(newstr, '\S.*?[\.\!\?]', 'match');
sentencesStrArray = string(sentences);
filePath = 'sentences.txt';
writelines(sentencesStrArray, filePath);
To store your sentences in a spreadsheet, you can use “cellstr” to convert the sentences into cell array and then store it in a CSV file.
sentencesCellArray = cellstr(sentences)';
% Specify the CSV file path
csvFilePath = 'sentences.csv';
% Write the cell array to the CSV file
writecell(sentencesCellArray, csvFilePath);
For more information about “regexp”, “cellstr” and “writecell”, you can refer the following documentation:
Hope this helps!
  댓글 수: 1
Stephen23
Stephen23 2024년 4월 28일
편집: Stephen23 2024년 4월 28일
Note that the superfluous type conversion using CELLSTR is easily avoided using WRITELINES instead of WRITECELL.
The comment "Removes all types of newline characters" is incorrect because NEWLINE is defined to be exactly one character: char(10). Nothing else. Although it could be argued that there are different types of newline character, the NEWLINE returns exactly one of them: char(10).
The code commented "For explicit \n characters" is of unclear benefit.
Also note that assuming periods end sentences is not always correct (but it might be for that data set).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by