The stated purpose for the fwrite function, according to the top of its documentation page, is to "Write data to binary file" (emphasis added.) But you're writing to a file with the extension .txt which suggests you're trying to write text data. For that purpose, fprintf would be a more appropriate choice if you want to using low-level file I/O functions.
But depending on exactly how you want the data to be formatted, a higher-level function may be more appropriate for this application. The csvwrite or dlmwrite functions may be useful, as might the save function with the -ascii qualifier.
As an additional FYI, there's a function newline that adds a newline character to a char or string array.
S = ['Hello' newline 'World']