필터 지우기
필터 지우기

define end of line (eol) as unix (\n) not Windows (\r\n) using writecell

조회 수: 63 (최근 30일)
Chris P
Chris P 2020년 12월 14일
편집: Chris P 2020년 12월 14일
I've got a cell of strings I'd like to write to a text file using the writecell funciton, however, the end of line (eol or newline or line break) is in Windows format (\r\n or CR LF) but I need it to be in Unix (\n or LF).
I'm using Windows OS.
Code to generate text file output:
my_cell_of_strings = [{"Some string"}; {"Another string"}];
writecell(my_cell_of_strings, 'testEOL.', 'FileType','text', 'QuoteStrings', false)
Then e.g. opening in Notepad++ I can see the format is Windows (CR LF) but I need Unix (LF)
I see dlmwrite has the Name-Value pair 'newline' (https://uk.mathworks.com/help/matlab/ref/dlmwrite.html#btzn85y-1-newline) but writecell doesn't seem to.
Are there any simple solutions?
(I can open in Notepad++ and convert eol but it's not really feasible for many files so needing to do it programatically).
(I could use a loop and fprintf etc. but like the convenience of writecell)
Thanks in advance!
Chris
  댓글 수: 2
Stephen23
Stephen23 2020년 12월 14일
WRITECELL is ultimately just a wrapper around WRITETABLE.
WRITETABLE calls matlab.io.internal.writing.writeToTextFile, which does not seem to have any option for changing the EOL character. Most likely Walter Roberson is the right person to ask about this.
Chris P
Chris P 2020년 12월 14일
Thanks, Stephen - I thought that'd be the case. Wonder if it might be included in future release, it'd be a good one-liner!

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2020년 12월 14일
What about writing directly?
C = [{"Some string"}; {"Another string"}];
fid = fopen('testEOL.txt', 'w');
fprintf(fid, '%s\n', C{:});
fclose(fid)
  댓글 수: 1
Chris P
Chris P 2020년 12월 14일
Thanks, Jan.
For some reason I handn't twigged that using the curly braces in fprint would work as neatly as it does - couple of extra lines vs writecell but I'll roll with it :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rik
Rik 2020년 12월 14일
편집: Rik 2020년 12월 14일
You can hack a solution together:
  1. Write the file with CRLF with writecell
  2. Read the file as a text file (e.g. with readfile or readlines)
  3. Write the files back with fprintf(fid,'%s\n',data{:}) to write with LF only (do not fopen with the t flag)
Not elegant, not efficient, but simple.
  댓글 수: 1
Chris P
Chris P 2020년 12월 14일
Thannks, Rik. Afraid I've gone for Jan's anwer though as it probably works best for me ...although your's does accurately answer my question!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by