How do I store the changing values of a variable (generated in a for loop) into a single file without overwriting the previous one?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
For example:
n = 100;
for ii = 1 : n
areatriangle = .5*((ii/2)*ii);
end
In the case above, I want to store all the outcomes/iterations of areatriangle, not just the last one. How do I do this?
Thanks in advance for your help!
Accepted Answer
Alex Mcaulley
on 30 Oct 2019
n = 100;
areatriangle = zeros(1,n)
for ii = 1 : n
areatriangle(ii) = .5*((ii/2)*ii);
end
or:
areatriangle = .5/2*(1:n).^2;
7 Comments
Thanks Alex. In my actual code (see below), I can't seem to get this to work for the output variables (i.e., ThetaInDegrees_a, ThetaInDegrees_b, ThetaInDegrees_c), where I need 10 angles for each pt (10 x 953). Can you help with me this? Thanks in advance for any help you could offfer.
close all;
clearvars;
load('F_points.mat');
load('fpep.mat');
xy = F_points';
n = 10; % Find the n closest values (excluding the point selected)
for pt = 1 : 953
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
adirector_x = ([fpep(pt,1) - fpep(pt,7)]);
adirector_y = ([fpep(pt,2) - fpep(pt,8)]);
bdirector_x = ([fpep(pt,3) - fpep(pt,7)]);
bdirector_y = ([fpep(pt,4) - fpep(pt,8)]);
cdirector_x = ([fpep(pt,5) - fpep(pt,7)]);
cdirector_y = ([fpep(pt,6) - fpep(pt,8)]);
vector_a = [adirector_x adirector_y];
vector_b = [bdirector_x bdirector_y];
vector_c = [cdirector_x cdirector_y];
chordx = ([xyNearest(:,1) - xy(pt,1)]);
chordy = ([xyNearest(:,2) - xy(pt,2)]);
chordxy = [chordx chordy];
for j = 1 : n
CosTheta_a(j) = dot(vector_a,chordxy(j,:))/(norm(vector_a)*norm(chordxy(j,:)));
CosTheta_b(j) = dot(vector_b,chordxy(j,:))/(norm(vector_b)*norm(chordxy(j,:)));
CosTheta_c(j) = dot(vector_c,chordxy(j,:))/(norm(vector_c)*norm(chordxy(j,:)));
ThetaInDegrees_a(j) = acosd(CosTheta_a(j));
ThetaInDegrees_b(j) = acosd(CosTheta_b(j));
ThetaInDegrees_c(j) = acosd(CosTheta_c(j));
end
end
In this case you need to store the result in a matrix
for j = 1 : n
CosTheta_a(j,pt) = dot(vector_a,chordxy(j,:))/(norm(vector_a)*norm(chordxy(j,:)));
CosTheta_b(j,pt) = dot(vector_b,chordxy(j,:))/(norm(vector_b)*norm(chordxy(j,:)));
CosTheta_c(j,pt) = dot(vector_c,chordxy(j,:))/(norm(vector_c)*norm(chordxy(j,:)));
ThetaInDegrees_a(j,pt) = acosd(CosTheta_a(j));
ThetaInDegrees_b(j,pt) = acosd(CosTheta_b(j));
ThetaInDegrees_c(j,pt) = acosd(CosTheta_c(j));
end
Don't forget to initialize every variable before the loops to increase the speed:
CosTheta_a = zeros(n,953)
%And so on
Steve
on 30 Oct 2019
Thanks again Alex. I tried running this change and it just gave me the first 10 angles 953 times. Any idea on what's going wrong? Thanks again!
It works for me! Where are you initializing the variables?
close all;
clearvars;
load('F_points.mat');
load('fpep.mat');
xy = F_points';
n = 10; % Find the n closest values (excluding the point selected)
CosTheta_a = zeros(n,953);
CosTheta_b = zeros(n,953);
CosTheta_c = zeros(n,953);
ThetaInDegrees_a = zeros(n,953);
ThetaInDegrees_b = zeros(n,953);
ThetaInDegrees_c = zeros(n,953);
for pt = 1 : 953
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
adirector_x = ([fpep(pt,1) - fpep(pt,7)]);
adirector_y = ([fpep(pt,2) - fpep(pt,8)]);
bdirector_x = ([fpep(pt,3) - fpep(pt,7)]);
bdirector_y = ([fpep(pt,4) - fpep(pt,8)]);
cdirector_x = ([fpep(pt,5) - fpep(pt,7)]);
cdirector_y = ([fpep(pt,6) - fpep(pt,8)]);
vector_a = [adirector_x adirector_y];
vector_b = [bdirector_x bdirector_y];
vector_c = [cdirector_x cdirector_y];
chordx = ([xyNearest(:,1) - xy(pt,1)]);
chordy = ([xyNearest(:,2) - xy(pt,2)]);
chordxy = [chordx chordy];
for j = 1 : n
CosTheta_a(j,pt) = dot(vector_a,chordxy(j,:))/(norm(vector_a)*norm(chordxy(j,:)));
CosTheta_b(j,pt) = dot(vector_b,chordxy(j,:))/(norm(vector_b)*norm(chordxy(j,:)));
CosTheta_c(j,pt) = dot(vector_c,chordxy(j,:))/(norm(vector_c)*norm(chordxy(j,:)));
ThetaInDegrees_a(j,pt) = acosd(CosTheta_a(j));
ThetaInDegrees_b(j,pt) = acosd(CosTheta_b(j));
ThetaInDegrees_c(j,pt) = acosd(CosTheta_c(j));
end
end
Steve
on 30 Oct 2019
I just copied your code and ran it; this is what I got for ThetaInDegrees_a (see screenshot):

Ohhh yes, you need to change the following lines in the last loop:
ThetaInDegrees_a(j,pt) = acosd(CosTheta_a(j,pt));
ThetaInDegrees_b(j,pt) = acosd(CosTheta_b(j,pt));
ThetaInDegrees_c(j,pt) = acosd(CosTheta_c(j,pt));
Steve
on 30 Oct 2019
Bingo! You're amazing. Thank you so much!
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)