How can I write some strings in text file (each strinng in one line)? example:
Happy
New
Year

 採用された回答

Image Analyst
Image Analyst 2013 年 12 月 24 日
編集済み: Image Analyst 2013 年 12 月 27 日

5 投票

Try this:
fid = fopen('ny.txt','wt');
fprintf(fid, 'Happy\nNew\nYear');
fclose(fid);

1 件のコメント

Image Analyst
Image Analyst 2013 年 12 月 24 日
編集済み: Image Analyst 2013 年 12 月 27 日
Or, if your strings are in variables,
fid = fopen('ny.txt','wt');
fprintf(fid, '%s\n%s\n%s', string1, string2, string3);
fclose(fid);

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

その他の回答 (5 件)

xingxingcui
xingxingcui 2022 年 9 月 5 日
編集済み: xingxingcui 2024 年 4 月 27 日

2 投票

hi, if you use latest matlab 2022a, you can use new matlab build-in function WRITELINES
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com
Walter Roberson
Walter Roberson 2013 年 12 月 24 日

0 投票

fid = fopen('ny.txt','wt');
fprintf(fid, 'Happy\New\n');
fprintf(fid,, 'Year\n');
fclose(fid)

2 件のコメント

Ronaldo
Ronaldo 2013 年 12 月 24 日
What I see in the text file is
HappyYear
What I want to see is
Happy
Year
Ronaldo
Ronaldo 2013 年 12 月 24 日
編集済み: Ronaldo 2013 年 12 月 27 日
The correct one is
fid = fopen('ny.txt','wt');
fprintf(fid, 'Happy\n');
fprintf(fid, 'New\n');
fprintf(fid, 'Year\n');
fclose(fid)
Anyway, so many thanks for your great help.

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

Dinesh Satelkar
Dinesh Satelkar 2017 年 2 月 9 日

0 投票

How i can write extracted text in text file

1 件のコメント

Image Analyst
Image Analyst 2017 年 2 月 9 日
With fprintf().

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

Ali
Ali 2017 年 8 月 3 日

0 投票

Is there a way to use save function to do that simple job? I could never get why an intuitive write in matlab needs such an un-intuitive function F-Print-F.

1 件のコメント

Image Analyst
Image Analyst 2017 年 8 月 3 日
That's just the name of it. It matches the name used in other languages like C, Java, etc. The save() function does not write custom formatted strings out to a text file like fprintf() does. The save() function saves variables in a binary, proprietary fashion.

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

Ali
Ali 2017 年 8 月 3 日
編集済み: Ali 2017 年 8 月 3 日

0 投票

Thanks for the comments. Save has a -ascii option which is supposed to do what I need. But now that you know about matlab, could you please comment on my question here: write multiple lines of text into a text file ?

1 件のコメント

Walter Roberson
Walter Roberson 2017 年 8 月 3 日
"Each variable must be a two-dimensional double array."
Character strings are not double array. double() of the string is done internally and that is written, getting back results such as
1.0400000e+02 1.0100000e+02 1.0800000e+02 1.0800000e+02 1.1100000e+02 9.2000000e+01 1.1000000e+02 1.1900000e+02 1.1100000e+02 1.1400000e+02 1.0800000e+02 1.0000000e+02
for 'hello\nworld'

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

カテゴリ

タグ

質問済み:

2013 年 12 月 24 日

編集済み:

2024 年 4 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by