MATLAB: Starting a new line while writing to a text file with fprintf()

\n fprintf text file

Hello,
I'm trying to write to a text file using fprintf, and using \n to skip to a new line doesn't seems to be working. If I use something like this:
cd('C:\');
fileid=fopen('testbatfile.txt','w')
a1='@echo'
a2='cd=directory'
formatSpec = '%1s\n\r%2s\n\r';
fprintf(fileid,formatSpec,a1,a2)
My result is a text file that appears:
@echocd=directory
Why isn't '\n' working here?
Thanks in advance.

Best Answer

You can open the file as 'wt' instead of 'w' and use \n instead of \n\r or you can open it the way you are now and use \r\n instead of \n\r