How to save values changing in a loop without overwriting?
조회 수: 3 (최근 30일)
이전 댓글 표시
I need every 100th value for
emag
to be saved against the corresponding value of t so that I can plot the results, however I have tried
dlmwrite('filename.txt', emag,'-append', '\t')
dlmwrite('filename.txt', t,'-append', '\t')
and it's not working for me and I'm not very well versed with MATLAB at all so any help would be very greatly appreciated. This is my loop;
m = 0;
for i = 1: m
m = m + 1;
if m == 100
C{i} = emag;
C2{i} = t;
E(i,1) = emag;
E(i,2) = t;
%m = 0;
t = t + steps*delta;
end
end
댓글 수: 5
Rik
2021년 4월 30일
The loop isn't, the code before it is.
%generate values to avoid errors
P=rand(2,20);V=rand(2,20);G=rand;M=rand;H=rand;t=rand;
D=dot(P(2,:), V(2,:))
e1=norm(V(2,:));
e1=e1^2
mu=G*M(1)
e2=e1/mu
r1=norm(P(2,:))
e3=e2/r1
e4=e3*P(2,:)
e5=D/mu
e6=e5*V(2,:)
e=e4-e6
emag=norm(e)
emag=emag/1.0e+10
%t = t + steps*delta;
steps = 100;
delta = 0.5;
update_plot(P,H,t)
m = 0;
for i = 1: m
m = m + 1;
if m == 100
C{i} = emag;
C2{i} = t;
E(i,1) = emag;
E(i,2) = t;
matrixToWrite = [t emag];
writematrix(matrixToWrite, 'myData.txt')
%m = 0;
t = t + steps*delta;
end
end
function update_plot(P,H,t)
end
답변 (1개)
Nagasai Bharat
2021년 5월 3일
Hi,
From my understanding you are trying to loop over and save every 100th element corresponding to 100th element is t.
The problem arising in the initialization of the for loop . In you code, the statements inside the loop are not excecuted due to incorrect use of for.
for i = a:b
Here while indexing an array i has to be an whole number and also b should be greater than a for the looping. There variables a and b should be already defined and not be changed. Other alternatives to use is the while loop as there a logical condition would be checked to loop further.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!