text file dlmwrite fprintf save
2 views (last 30 days)
Show older comments
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..
0 Comments
Answers (2)
Walter Roberson
on 27 Jul 2011
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);
Oleg Komarov
on 27 Jul 2011
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 Comments
Oleg Komarov
on 27 Jul 2011
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).';
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!