How to save values changing in a loop without overwriting?

8 views (last 30 days)
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 Comments
Lauren Miller
Lauren Miller on 30 Apr 2021
It definitely is printing values to the command window,
The whole block of code is
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;
Rik
Rik on 30 Apr 2021
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,:))
D = 4.1719
e1=norm(V(2,:));
e1=e1^2
e1 = 5.6835
mu=G*M(1)
mu = 0.5082
e2=e1/mu
e2 = 11.1826
r1=norm(P(2,:))
r1 = 2.7853
e3=e2/r1
e3 = 4.0148
e4=e3*P(2,:)
e4 = 1×20
1.2742 2.4955 3.5995 3.0196 2.2990 0.7363 3.3123 0.1011 2.8078 1.7513 3.4836 3.5537 1.2469 3.0465 3.8202 1.7159 0.5303 1.6641 3.1727 1.0536
e5=D/mu
e5 = 8.2083
e6=e5*V(2,:)
e6 = 1×20
3.9968 0.9303 7.7538 0.9089 1.7754 7.5097 4.9340 5.2943 1.7248 5.1817 0.4586 4.4645 3.1174 4.0714 1.0209 2.6601 7.5543 1.4035 0.9719 6.9904
e=e4-e6
e = 1×20
-2.7226 1.5652 -4.1542 2.1106 0.5236 -6.7734 -1.6217 -5.1932 1.0829 -3.4304 3.0250 -0.9108 -1.8706 -1.0249 2.7993 -0.9442 -7.0240 0.2607 2.2008 -5.9368
emag=norm(e)
emag = 15.2649
emag=emag/1.0e+10
emag = 1.5265e-09
%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

Sign in to comment.

Answers (1)

Nagasai Bharat
Nagasai Bharat on 3 May 2021
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.

Categories

Find more on Loops and Conditional Statements 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!