필터 지우기
필터 지우기

Write a 2D character array to textual file

조회 수: 5 (최근 30일)
Marko Gulin
Marko Gulin 2017년 9월 6일
편집: Marko Gulin 2017년 9월 6일
Hy everyone,
What would be the best way in terms of computational time to write a 2D character array from Matlab to a textual file?
I've used this:
dlmwrite('file.txt', data, '');
but it is too slow.
I want to write a 2D character array so that each array row is printed on a new line.
Thanks!
  댓글 수: 1
Stephen23
Stephen23 2017년 9월 6일
Probably the fastest way would be to use fprintf directly. Have you tried that?

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

채택된 답변

Cam Salzberger
Cam Salzberger 2017년 9월 6일
편집: Cam Salzberger 2017년 9월 6일
Hello Marko,
The most easily read code would probably be to either stick with dlmwrite, or loop through each row and use fprintf on an open file. However, if you want to avoid loops, you could do it like this:
fid = fopen('test.txt','w');
Anew = [A repmat(newline,size(A,1),1)];
fprintf(fid,'%s',Anew.');
fclose(fid);
Appending newlines on the ends of the rows makes it unnecessary to loop row by row. Transposing the array before writing it is necessary because fprintf works in linear indexing order (column-major).
-Cam
  댓글 수: 2
Stephen23
Stephen23 2017년 9월 6일
편집: Stephen23 2017년 9월 6일
A slightly clearer way to add the newline:
Anew = A;
Anew(:,end+1) = newline;
Marko Gulin
Marko Gulin 2017년 9월 6일
편집: Marko Gulin 2017년 9월 6일
Thank you, all of these answers have helped me :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by