TXT export
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello community,
I have a short and simple question. I want to export one vector H and Matrix Ysim to a txt file. This works so far. Furthermore I would like to name the column of the Ysim Matrix in the txt. Any ideas? Thanks
Example:
H=[1;2;3] Ysim=[1,2,3;4,5,6;7,8,9]
dlmwrite('elecdat.txt',[H,Ysim], 'delimiter', '\t', 'precision', 5)
Sarima
댓글 수: 0
답변 (2개)
Richard
2012년 5월 1일
I would suggest using fprintf instead of dlmwrite:
In order to name the columns in your text files you need to write them as strings and then use fprintf to export them into a text file.
댓글 수: 0
Richard
2012년 5월 2일
This code may give you some ideas:
clear all
a = 1;
b = 20;
data = a + (b-a).*rand(100,4);
Heading = {'Data'};
filename = 'E:\University\CEH Lancaster\test.txt';
fid = fopen(filename,'wt');
fprintf(fid,'%s\r\n',Heading{1});
for i = 1:length(data);
fprintf(fid,'%f\t%f\t%f\t%f\r\n',data(i,:));
end
fclose all
댓글 수: 2
Jan
2012년 5월 2일
And to be more user-firendly, omit the "clear all" and do not close all open files, but only "fclose(fid)". Remember that this piece of code should be useful in a greater background also.
Another idea is the vectorized usage of FPRINTF instead of the loop over i:
fprintf(fid,'%f\t%f\t%f\t%f\r\n', transpose(data));
If you open a file using the 'wt' format, you do not have to specify '\r\n' as line break. Either use '\n', or open the file in the 'w' format.
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!