write string in text file

574 Ansichten (letzte 30 Tage)
Ronaldo
Ronaldo am 24 Dez. 2013
Verschoben: Stephen23 am 5 Sep. 2022
How can I write some strings in text file (each strinng in one line)? example:
Happy
New
Year

Akzeptierte Antwort

Image Analyst
Image Analyst am 24 Dez. 2013
Bearbeitet: Image Analyst am 27 Dez. 2013
Try this:
fid = fopen('ny.txt','wt');
fprintf(fid, 'Happy\nNew\nYear');
fclose(fid);
  3 Kommentare
Ronaldo
Ronaldo am 27 Dez. 2013
The last line must be changed to:
fclose(fid);
Image Analyst
Image Analyst am 27 Dez. 2013
Right Ronaldo - thanks for noticing and correcting. I've edited and corrected.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

cui,xingxing
cui,xingxing am 5 Sep. 2022
Verschoben: Stephen23 am 5 Sep. 2022
hi, if you use latest matlab 2022a, you can use new matlab build-in function WRITELINES

Walter Roberson
Walter Roberson am 24 Dez. 2013
fid = fopen('ny.txt','wt');
fprintf(fid, 'Happy\New\n');
fprintf(fid,, 'Year\n');
fclose(fid)
  2 Kommentare
Ronaldo
Ronaldo am 24 Dez. 2013
What I see in the text file is
HappyYear
What I want to see is
Happy
Year
Ronaldo
Ronaldo am 24 Dez. 2013
Bearbeitet: Ronaldo am 27 Dez. 2013
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.

Melden Sie sich an, um zu kommentieren.


Dinesh Satelkar
Dinesh Satelkar am 9 Feb. 2017
How i can write extracted text in text file

Ali
Ali am 3 Aug. 2017
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 Kommentar
Image Analyst
Image Analyst am 3 Aug. 2017
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.

Melden Sie sich an, um zu kommentieren.


Ali
Ali am 3 Aug. 2017
Bearbeitet: Ali am 3 Aug. 2017
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 Kommentar
Walter Roberson
Walter Roberson am 3 Aug. 2017
"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'

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by