Filter löschen
Filter löschen

How to store char to a text file?

82 Ansichten (letzte 30 Tage)
Mark Golberg
Mark Golberg am 21 Okt. 2022
Kommentiert: Mathieu NOE am 24 Okt. 2022
Hello,
I have the input text I want to save into a *.txt file.
Please see an example is in the included mat file.
It looks like this:
-------------------------------------
val =
av Gain Corrected [[%]] --> 0.32795 (-Fail-)
av Non Gain Corrected [[%]] --> 0.11452
P0 [count] --> 0
P1 [count] --> 1.0643
P2 [count] --> -1.1197e-05
P3 [count] --> 6.5011e-10
P4 [count] --> -1.5386e-14
P5 [count] --> 1.2889e-19
-----------------------------------
How can I store this variable (only the bold test) into *.txt file, so it will maintain all the "next line" / special charachters, etc..
I've attached a required_output.txt (created by a simply copy-paste).
* I've tried several solutions suggested on the web, but each time something else is messed up... :(
** Using MATLAB 2015b (for legacy reasons, can't advance to any newer versions)
THANKS!!!

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 21 Okt. 2022
hello Mark
try this ; your char array must be first transformed in string , otherwise this code will not work
hope it helps
load('input_text.mat')
filename_out = 'output.txt';
fid = fopen(filename_out, 'w' ); %// open file to writing
fprintf( fid, '%s\n', string(results_txt) ); %// print string to file
fclose( fid ); %// don't forget to close the file
disp(['File :' filename_out ' has been saved.']);
File :output.txt has been saved.
type('output.txt')
av Gain Corrected [[%]] --> 0.32795 (-Fail-) av Non Gain Corrected [[%]] --> 0.11452 P0 [count] --> 0 P1 [count] --> 1.0643 P2 [count] --> -1.1197e-05 P3 [count] --> 6.5011e-10 P4 [count] --> -1.5386e-14 P5 [count] --> 1.2889e-19
  6 Kommentare
Mark Golberg
Mark Golberg am 21 Okt. 2022
with dlmwrite it worked perfectly.
THANK YOU !!!
Mathieu NOE
Mathieu NOE am 24 Okt. 2022
My pleasure !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 21 Okt. 2022
fopen the file with 'w' access. fwrite() the character vector. fclose()

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by