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

3 次查看(过去 30 天)
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

回答(1 个)

Image Analyst
Image Analyst 2014-4-23
Make the badly-named "a" a 2D array and put a single call to dlmwrite() AFTER the two loop s.
  3 个评论
Image Analyst
Image Analyst 2014-4-25
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 2014-4-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MuPAD 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by