how to write a matrix to text file with loop name of .txt

조회 수: 3 (최근 30일)
ha ha
ha ha 2019년 5월 14일
댓글: madhan ravi 2019년 5월 14일
Let's say, I have a code like this:
for i = 1:size(cell,2)
data = cell{i}; %kx3 matrix
dlmwrite('name_change_acoording_to_each_loop.txt', data, 'delimiter','\t','newline','pc','precision',15);
end
I want to write a matrix to .txt file. And The result will be a set of .txt file, with the name is according to the value of i
Example:
cell={[1 1 1] [1 2 3;3 2 1] [4 5 6;7 8 9;1 1 1] [2 2 2;3 3 3]};
-Loop 1: i=1 ---> the result of text file is : 1.txt
-Loop 2: i=2 ---> the result of text file is : 2.txt
-Loop 3: i=3 ---> the result of text file is : 3.txt
-Loop 4: i=4 ---> the result of text file is : 4.txt
Finally, I will have 4 text files with the name is: 1.txt ; 2.txt ; 3.txt; 4.txt
But my code doesn't provide the result as my expectation.
How can I do that? How can I fix my code? Thanks

채택된 답변

madhan ravi
madhan ravi 2019년 5월 14일
sprintf('%d.txt',i) % this would be your filename inside the loop
  댓글 수: 2
ha ha
ha ha 2019년 5월 14일
편집: ha ha 2019년 5월 14일
You mean my modified code like this:
for i = 1:size(cell,2)
data = cell{i}; %kx3 matrix
dlmwrite('name_change_acoording_to_each_loop.txt', data, 'delimiter','\t','newline','pc','precision',15);
sprintf('%d.txt',i) % this would be your filename inside the loop
end
madhan ravi
madhan ravi 2019년 5월 14일
for i = 1:size(cell,2)
data = cell{i}; %kx3 matrix
filename=sprintf('%d.txt',i); % this would be your filename inside the loop
dlmwrite(filename, data, 'delimiter','\t','newline','pc','precision',15);
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by