hi:
I have data with different column, such as
a=ones(100,2), b=ones(50,2);
but I want to append them and export to a .txt file, so what I did is:
- construct a new matrix that has the size (max row of a&b, sum of the column of a&b), here is with the size: (100,4).
- for each row, find the data that equal to '1', and build a new row that set all the element '1' to ' ,', and save to .txt file using fprinf. however, I found that it is too slow.
below is my test code, and I also attatched the test file A.
load Aticfid = fopen('test.txt','w'); for i=1:1:length(A(:,1)) str={''}; for j=1:1:length(A(1,:)) if A(i,j)==1e5; str=strcat(str,{' ,'}); else str=strcat(str,sprintf('%0.5e',A(i,j)),','); end end fprintf(fid,'%s\n',str{1}); end fclose(fid);tocticdlmwrite('test_dlm.txt',A)toc
here the 1e5 is the identifier that need to be set to ' ,'. the result shows that fprintf will cost about 129 seconds, while the dlmwrite cost only 12 seconds.
thanks!
Li
Best Answer