how to create a .txt file from char vector using matlab?

76 ビュー (過去 30 日間)
Osama Alkurdi
Osama Alkurdi 2020 年 2 月 20 日
コメント済み: Osama Alkurdi 2020 年 2 月 20 日
let us say that I have this char vector T=['x=2;',newline,'y=3;',newline,'z=x*y;'];
I want to convert T to txt file and save it in a certain directory
is that possible using matlab?

採用された回答

Adam Danz
Adam Danz 2020 年 2 月 20 日
編集済み: Adam Danz 2020 年 2 月 20 日
T=['x=2;',newline,'y=3;',newline,'z=x*y;'];
filename = 'myTextFile.txt'; % better to use fullfile(path,name)
fid = fopen(filename,'w'); % open file for writing (overwrite if necessary)
fprintf(fid,'%s',T); % Write the char array, interpret newline as new line
fclose(fid); % Close the file (important)
open(filename) % Open the text file in the editor
Note, if you want to open the file in Notepad or some other editor that doesn't interpret the newline character correctly, you use this line below instead of the other fopen() line above.
fid = fopen(filename,'wt');
  3 件のコメント
Adam Danz
Adam Danz 2020 年 2 月 20 日
I'm glad it was helpful.
Just a head's up: If you searched the internet for "Matlab how to write character array to text file" you would have found several approaches to try. Often times "googling" the term "matlab" plus your question will provide you with many starting points.
Osama Alkurdi
Osama Alkurdi 2020 年 2 月 20 日
thank you for the advice
I appreciate it :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by