Dlmwrite in looping - only records the last Linha of the matrix in the text file

1 view (last 30 days)
Hi
This is my script:
for n=1:numel(LonUq)
for m = 1:numel(LatUq)
ind = find(LLDAm(:,2) == LonUq(n) & LLDAm(:,1) == LatUq(m));
if (isempty(ind)), continue; end
p = polyfit([0; agemodel(ind)],[0; depth(ind)], 1);
idade_int(m,n) = polyval(p, 20);
a = [LatUq(m), LonUq(n), idade_int(m,n)];
disp(a);
dlmwrite('saveteste.txt', a , 'delimiter','\t', 'precision', 4 )
end
end
but when, i record txt file, appears only the last line that was executed in the cycle ( a ). While in the command window pops up all the lines.
For example:
Txt file: (a)
20.2917 -18.2617 1.5621
Command Window: (a)
30.7492 -20.0000 0.8333
20.7492 -18.6808 5.0000
20.7492 -18.5808 5.0451
30.7492 -18.5808 5.0000
20.2917 -18.2617 1.5621
20.2917 -18.2617 1.5621
Thanks

Answers (1)

Image Analyst
Image Analyst on 23 Apr 2014
Make the badly-named "a" a 2D array and put a single call to dlmwrite() AFTER the two loop s.
  3 Comments
Image Analyst
Image Analyst on 25 Apr 2014
Why are doing [(m*n), 3]???? Does that even work at all? DOesn't even look like correct syntax. Try
aa = zeros(numel(LatUq), numel(LonUq), 3); % Initialize before the loop.
Then in the loop:
aa(m, n, :) = [LatUq(m), LonUq(n), idade_int(m,n)];
Nuno Simões
Nuno Simões on 25 Apr 2014
Yes, that works.
But it shows this:
0 0 0 20.29 0 0 0 -18.26 0 0 0 1.562
0 20.75 20.75 0 0 -18.68 -18.58 0 0 5 5.045 0
30.75 0 30.75 0 -20 0 -18.58 0 0.8333 0 5 0
and not what I want:
30.7492 -20.0000 0.8333
20.7492 -18.6808 5.0000
20.7492 -18.5808 5.0451
30.7492 -18.5808 5.0000
20.2917 -18.2617 1.5621
20.2917 -18.2617 1.5621
Sorry, but i am very confuse.
Thank you very much.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!