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!!!

 採用された回答

Mathieu NOE
Mathieu NOE 2022 年 10 月 21 日

1 投票

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 件のコメント

Mark Golberg
Mark Golberg 2022 年 10 月 21 日
@Mathieu NOE thank you.
unfortunately I get the following error:
--------------------------------------
Error using string
string is obsolete and will be discontinued. Use char instead.
----------------------------------------
If I use "char" instead (although, my test is originally char) I get a one long line of gibrish...
Something like that:
----------
aaPPPPPPvv012345 GN[[[[[[aoccccccinoooooon uuuuuu GnnnnnnCattttttoi]]]]]]rn r ------eC------co>>>>>>tr er01-6-1de .1.1. c 0.5.2[t 61058[e 41138%d 39189] 7e6e][ e-e- [ -1-1-% 0019-] 5 4 >] 0- .- 3> 2 70 9. 51 1 (4 -5 F2 a i l - )
-------------
How did you manage to get such a nice output file? Am I missing something?
Mathieu NOE
Mathieu NOE 2022 年 10 月 21 日
yes , seems TMW has for some obscur reasons decided that strings are not anymore up to date
(I am running R2020b so it's still doable)
try with a quite old function dlmwrite , it still works for me
filename_out = 'output.txt';
dlmwrite(filename_out,results_txt,'delimiter','');
type('output.txt')
Walter Roberson
Walter Roberson 2022 年 10 月 21 日
string objects did not exist in the user's old version.
Mathieu NOE
Mathieu NOE 2022 年 10 月 21 日
thanks for the clarification, I was myself puzzled that strings would disappear
Mark Golberg
Mark Golberg 2022 年 10 月 21 日
with dlmwrite it worked perfectly.
THANK YOU !!!
Mathieu NOE
Mathieu NOE 2022 年 10 月 24 日
My pleasure !

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 10 月 21 日

0 投票

fopen the file with 'w' access. fwrite() the character vector. fclose()

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2015b

タグ

質問済み:

2022 年 10 月 21 日

コメント済み:

2022 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by