Search code examples
matlabencodingcarriage-return

Set carriage return type for writematrix


I am trying to write data to a text file using writematrix.

 writematrix([ 1 2 3 4 5 6]' ,'patate.txt')

MATLAB encodes it by default as UTF-8, and uses a Windows style return (CR LF) (example in Notepad++) :

CR LF in notepad

The name-value pair 'Encoding' can be passed to writematrix, but the documentation is not explicit on how to use it for the system type:

 writematrix([ 1 2 3 4 5 6]' ,'patate.txt', 'Encoding','UTF-8')

Is it possible to configure writematrix, or use another function, to have UTF-8 encoding but with Unix style returns (LF)?


Solution

  • I believe that I cannot change the carriage return to Unix format writematrix.

    Thanks to Adriaan's comment I looked deeper into the source of writematrix. A some point, writeTextFile is called and calls fopen here:

    % Open the file for writing
    if strcmp(encoding,'system')
        [fid,errmsg] = fopen(file,'Wt'); % text mode: CRLF -> LF
    else
        [fid,errmsg] = fopen(file,'Wt','n',encoding); % text mode: CRLF -> LF
    end
    

    The encoding parameter will be used in the else condition and fopen is called with a default parameter set to 'n', which means 'native' or 'n' - local machine format - the default

    Therefore I cannot force the carriage return style.