Image reloading in code - 2 point azimuth

1 view (last 30 days)
I have the following code that allows me to calculate two-point azimuth between geographic features from an XY table (courtesy of Nicolas LeCorvec). However, instead of loading multiple files, I only need to run it for a single file (WP.text - see attached). Upon running, Matlab takes a very long time and appears stuck in a loop. Using a simple input file (test.text - see attached), you can see what happens: the figure gets plotted time after time. How do I prevent this from happening by adjusting the following code:
%Nicolas LE CORVEC
%Script allwoing the creation of alignments using 3 points
%Auckland 2009
%--------------------------------------------------------------------------
clear all
list_files2load = dir('*.txt'); %put all the .txt files into list_files2load
files = {list_files2load.name};
m = length(files);
% Loop for each volcanic field
for w=1:m
sprintf('loading file : %s', files{w})
s = load(files{w});
File=files{w};
[FilePath,FileName,FileExt]=fileparts(File);
I=zeros(3,3); %preallocation for the number of regression lines created
clear P
C=10:5:20; %distance/error between the points ot the line (t in the manuscript)
% Loop for the distance of points from the regression line
for c=1:length(C)
%The distance max is a file created for each volcanic fields since
%they all have a different density and mean nearest neighbor distance
N=1000:500:3000;
% Loop for the distance between the points
for n=1:length(N)
I(c,n)=0;
x= s (1:end,1);
y= s (1:end,2);
% The figure for every N has to be clean otherwise the code is
% adding the same lines over and over the figure as n increases.
clf %clear the figure
% plot the location of the volcanoes
plot(x,y,'+','MarkerSize',4)
axis equal
hold on
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
for i=1:numel(x)-2
for j=i+1:numel(x)-1
for k=j+1:numel(x)
distij=sqrt((x(j)-x(i))^2+(y(j)-y(i))^2);
distik=sqrt((x(k)-x(i))^2+(y(k)-y(i))^2);
distjk=sqrt((x(k)-x(j))^2+(y(k)-y(j))^2);
max([distij distik distjk]);
if max([distij distik distjk])<N(n) % The max length between two points (L in the manuscript)
%regression
[p, S] = polyfit([x(i),x(j),x(k)],[y(i),y(j),y(k)],1);
[y2,delta] = polyval(p,[x(i),x(j),x(k)],S);
indix = setdiff(1:numel(x),[i,j,k]);
dist = (-y(indix)+p(1)*x(indix)+p(2))/sqrt(p(1)^2+1);
dist2 = (-y+p(1)*x+p(2))/sqrt(p(1)^2+1);
% t length
[a,b]= sort(abs(dist));
max(abs([dist2(i),dist2(j),dist2(k)]));
if max([dist2(i),dist2(j),dist2(k)])<C(c) % this is the max distance from the 3 points to the line
%plots the lineament in red, and the
%volcanoes used to create the line in green
plot([x(i),x(j),x(k)],y2,'r-',[x(i),x(j),x(k)],[y(i),y(j),y(k)],'g*','MarkerSize',2);
%plots only the lineament in red
plot([x(i),x(j),x(k)],y2,'r-');
%Save Figure as Illustrator
Name = sprintf('%s-%d-%d', FileName, C(c), N(n));
%you need to change the directory of where you want to save the figure
saveas(gcf, fullfile('C:','Users','17391940','Desktop','Matlab', Name), 'jpg')
%number of lines created
I(c,n)=I(c,n)+1;
% Calculate the azimuth for each alignment
A=(x(k)-x(i));
B=(y2(3)-y2(1));
P(I(c,n),n)=atand(A/B);
if B<0, P(I(c,n),n)=P(I(c,n),n)+180;
elseif A<0, P(I(c,n),n)=P(I(c,n),n)+360;
end
if P(I(c,n),n)>180, P(I(c,n),n)=P(I(c,n),n)-180;
end
end
end
end
end
end
end
%Saving the azimuth into xls file
z= exist('P','var');
if z>0
l1=1;
Namesss = sprintf('%s-%d-%d-%d', FileName, l1, C(c), N(n));
xlswrite(Namesss, P)
end
end
%Saving the number of lines into an xls file
l2=2;
Namessss = sprintf('%s-%d-%d-%d', FileName, l2, C(c), N(n));
xlswrite(Namessss, I)
end
Thanks

Answers (0)

Community Treasure Hunt

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

Start Hunting!