필터 지우기
필터 지우기

text file dlmwrite fprintf save

조회 수: 2 (최근 30일)
Nicolas
Nicolas 2011년 7월 27일
Hi,
It might be another question on this subject.. but i'm confused saving text files.
i have a basic code with data(2,49) to save in each loop the data minus one row
i=length(data)
for j=1:i
coord=[data(1:(i-j),1),data(1:(i-j),2)];
filename = [ 'coord' num2str(j-i) '.txt' ];
Then I'm stuck..
dlmwrite (filename, coord,'delimiter','\t','precision',7)
end
do not write in column, I understood that text files are row oriented, but i don't get the fprintf way..
if someone can explain me..

답변 (2개)

Walter Roberson
Walter Roberson 2011년 7월 27일
You are not using fprintf() directly anywhere.
Anyhow, try
dlmwrite (filename, transpose(coord),'delimiter','\t','precision',7)
By the way: your coord matrix is not 2 x 49: it is 49 x 2.
Also, you can abbreviate your creation of coord:
coord = data(1:i-j,1:2);
  댓글 수: 1
Nicolas
Nicolas 2011년 7월 27일
thanks, I put fprintf() because I saw some answers written using it. Thanks for the abbreviation hint! but transpose don't work. When i open the text file the data are still on a row.

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


Oleg Komarov
Oleg Komarov 2011년 7월 27일
The version with fprintf:
% Sample data
data = rand(49,2);
szData = size(data);
% Generate all filenames at once
filename = reshape(sprintf('coord-%02d.txt', szData(1)-1:-1:0),12,49).';
% Loop and use fprintf
for n = 1:szData(1)
coord = data(1:szData(1)-n,:);
fid = fopen(filename(n,:),'w');
fprintf(fid,'%.7f\t%.7f\r\n',coord.');
fid = fclose(fid);
end
  댓글 수: 2
Nicolas
Nicolas 2011년 7월 27일
??? Error using ==> reshape
To RESHAPE the number of elements must not change.
Error in ==> Generate_sequence at 6
filename = reshape(sprintf('Beb-coord-%02d.txt', szData(1)-1:-1:0),12,49).';
Oleg Komarov
Oleg Komarov 2011년 7월 27일
You have to replace the 12 by the length of the name:
Beb-coord-48.txt, i.e. 16 chars.
filename = reshape(sprintf('Beb-coord-%02d.txt', szData(1)-1:-1:0),16,49).';

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

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by