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.

1 回表示 (過去 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.
  2 件のコメント
DGM
DGM 2024 年 4 月 17 日
Have you checked those strings to see if they contain newline characters?
Stephen23
Stephen23 2024 年 4 月 17 日
Remove the newlines and carriage returns from the text array.

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

採用された回答

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 Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by