MATLAB: How to create a .txt file from char vector using matlab

convertMATLABsavevector

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?

Best Answer

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');
Related Question