MATLAB: if i define this variable then all my data in that variable are being overwritten [closed]
i am writing a code in matlab about drawing triangles. there are some nodes i my graph and each time, i plot a line using 2 nodes, i am storing those node values in a certain way, inside a matrix called main. now if i define main at the begining of the program, then after 1 loop is completed, main is taking the initial value, and it is overwriting all my main values that was inside main while in loop. please suggest me a way to store all values in main and to define it also. please answer ASAP. this is my code
%% loading an image suitable for forming grid
base_image = imread('grid.png');
image(base_image)
%% inputing the nodes of 4x4 grid
[X,Y] = ginput(16);
pts = [X,Y];% defining all points
p = plot(pts(:,1),pts(:,2),'c.','MarkerSize',20);
hold on
%% generating the distances from each node
%% defining the points x and y coordinate
a = pts(:,1);
b = pts(:,2);
%% evaluating the conditions for choice1 and choice2
k = 0;
L = zeros(60,2);
for k = 1:60
%% the menu for choosing the nodes
choice1 = menu('nodes','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
choice2 = menu('nodes','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
%% distance of all nodes from the 2 choosen node
fd_dist_ch1 = zeros(16,1);
fd_dist_ch2 = zeros(16,1);
for i = 1:16
fd_dist_ch1(i) = sqrt((a(i)-a(choice1))^2 + (b(i)-b(choice1))^2);
end
for j = 1:16
fd_dist_ch2(j) = sqrt((a(j)-a(choice2))^2 + (b(j)-b(choice2))^2);
end
%% plotting the user_choice
p1 = plot([a(choice1) a(choice2)],[a(choice1) a(choice2)]);
set(p1,'Color','red','LineWidth',2)
hold on
L = (choice1*10)+choice2% this is the user input data saved in list L
main = cat(2,main,L)
pause(3)
L1 = (choice1-1)*10+choice2;
L2 = (choice1+1)*10+choice2;
if find(choice1-1)== 1% if there exists a node which can be node(choice1-1)
if ismember(L1,main)~= 0
p2 = plot([a(choice1) a(choice1-1)],[b(choice1) b(choice1-1)]);
set(p2,'Color','blue','LineWidth',2)
L2 = (choice1*10)+choice1-1;
L3 = ((choice1-1)*10)+choice1;
main = cat(2,main,L2,L3)
fill([a(choice1) a(choice1-1) a(choice2)],[b(choice1) b(choice1-1) b(choice2)],'b')
hold on
elseif find(choice1+1)== 1
if ismember(L2,main)~= 0
p3 = plot([a(choice1) a(choice1+1)],[b(choice1) b(choice1+1)]);
set(p3,'Color','blue','LineWidth',2)
L4 = (choice1*10)+choice1+1;
L5 = ((choice1+1)*10)+choice1;
main = cat(2,main,L4,L5)
fill([a(choice1) a(choice1+1) a(choice2)],[b(choice1) b(choice1+1) b(choice2)],'b')
hold on
else
p4 = plot([a(choice1+1) a(choice2+1)],[b(choice1+1) b(choice2+1)]);
set(p4,'Color','blue','LineWidth',2)
L5 = ((choice1+1)*10)+choice1+2
L6 = ((choice1+2)*10)+choice1+1
main = cat(2,main,L5,L6)
disp('yay')
end
end
end
end
i am writing a code in matlab about drawing triangles. there are some nodes i my graph and each time, i plot a line using 2 nodes, i am storing those node values in a certain way, inside a matrix called main. now if i define main at the begining of the program, then after 1 loop is completed, main is taking the initial value, and it is overwriting all my main values that was inside main while in loop. please suggest me a way to store all values in main and to define it also. please answer ASAP. this is my code
%% loading an image suitable for forming grid
base_image = imread('grid.png');
image(base_image)
%% inputing the nodes of 4x4 grid
[X,Y] = ginput(16);
pts = [X,Y];% defining all points
p = plot(pts(:,1),pts(:,2),'c.','MarkerSize',20);
hold on
%% generating the distances from each node
%% defining the points x and y coordinate
a = pts(:,1);
b = pts(:,2);
%% evaluating the conditions for choice1 and choice2
k = 0;
L = zeros(60,2);
for k = 1:60
%% the menu for choosing the nodes
choice1 = menu('nodes','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
choice2 = menu('nodes','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
%% distance of all nodes from the 2 choosen node
fd_dist_ch1 = zeros(16,1);
fd_dist_ch2 = zeros(16,1);
for i = 1:16
fd_dist_ch1(i) = sqrt((a(i)-a(choice1))^2 + (b(i)-b(choice1))^2);
end
for j = 1:16
fd_dist_ch2(j) = sqrt((a(j)-a(choice2))^2 + (b(j)-b(choice2))^2);
end
%% plotting the user_choice
p1 = plot([a(choice1) a(choice2)],[a(choice1) a(choice2)]);
set(p1,'Color','red','LineWidth',2)
hold on
L = (choice1*10)+choice2% this is the user input data saved in list L
main = cat(2,main,L)
pause(3)
L1 = (choice1-1)*10+choice2;
L2 = (choice1+1)*10+choice2;
if find(choice1-1)== 1% if there exists a node which can be node(choice1-1)
if ismember(L1,main)~= 0
p2 = plot([a(choice1) a(choice1-1)],[b(choice1) b(choice1-1)]);
set(p2,'Color','blue','LineWidth',2)
L2 = (choice1*10)+choice1-1;
L3 = ((choice1-1)*10)+choice1;
main = cat(2,main,L2,L3)
fill([a(choice1) a(choice1-1) a(choice2)],[b(choice1) b(choice1-1) b(choice2)],'b')
hold on
elseif find(choice1+1)== 1
if ismember(L2,main)~= 0
p3 = plot([a(choice1) a(choice1+1)],[b(choice1) b(choice1+1)]);
set(p3,'Color','blue','LineWidth',2)
L4 = (choice1*10)+choice1+1;
L5 = ((choice1+1)*10)+choice1;
main = cat(2,main,L4,L5)
fill([a(choice1) a(choice1+1) a(choice2)],[b(choice1) b(choice1+1) b(choice2)],'b')
hold on
else
p4 = plot([a(choice1+1) a(choice2+1)],[b(choice1+1) b(choice2+1)]);
set(p4,'Color','blue','LineWidth',2)
L5 = ((choice1+1)*10)+choice1+2
L6 = ((choice1+2)*10)+choice1+1
main = cat(2,main,L5,L6)
disp('yay')
end
end
end
end
No comments:
Post a Comment