Using viscircles to draw circles around points.
Show older comments
Hello, I am trying to draw to circles using viscircles but some of the circles end up in random space instead of around the center coordiantes ive given them. Below is the code I am using to draw my circles and a picture of what is happening on the plot. Any help would be greatly appreciated
% draw exlcusion range circles b/w hormone seeds and hormone seeds
HS_kept_x = HS_kept(:, 1);
HS_kept_y = HS_kept(:, 2);
LengthHS_kept = length(HS_kept);
radii_node2 = 8;
hold on
for i = 1:n
centers_node5 = [x(i), y(i)];
elim_circles3(i) = viscircles(centers_node5, radii_node2, 'color', 'k', 'linestyle', '--');
end
for j = 1:LengthHS_kept
centers_HS_kept = [HS_kept_x(j), HS_kept_y(j)];
elim_circles4(j) = viscircles(centers_HS_kept, radii_node2, 'color', 'k', 'linestyle', '--');
end

20 Comments
Adam Danz
on 23 Aug 2019
We don't have enough information. The problem is clearly with the center points of the circles. Center points are defined by x,y, HS_kept_x, and HS_kept_y but we don't have those values (along with some other variables). Could you provide a mat file that contains all the needed variables?
One red-ish flag that often causes problems is the use of length() instead of numel() or size(a,b). With length() you can't be sure which dimension it will return if the input is a matrix that potentially varies in size.
Another possibility is that your code is working fine but the inputs are the problem.
Vance Blake
on 24 Aug 2019
Adam Danz
on 24 Aug 2019
Again, that's not enough information. Remember that we don't know what "t' 'r' 'x' 'y' are or how they relate to your code. The x and y variables are also in your code so I suppose they are the same x and y variables in the txt files but there's still not enough info to run your code and troubleshoot the problem.
What we need is something we can download (or copy) and then run it without having to guess how you're loading the data, what variable names you're assigning or the form they take in your code, etc.
Vance Blake
on 24 Aug 2019
Edited: Vance Blake
on 25 Aug 2019
ahhh... there we go. The code breaks when we get to "HS_kept" because we don't have that variable and it's a critical one to have for troubleshooting the problem.
I already have some feedback but I'll wait until I can help out with the main problem.
Vance Blake
on 25 Aug 2019
Adam Danz
on 25 Aug 2019
The code still breaks at the same line. Have you tried to run the code you shared?
Vance Blake
on 26 Aug 2019
Edited: Vance Blake
on 26 Aug 2019
Ok, let's solve this together. That way you get to learn how to do some of the troubleshooting.
The code above runs without error. I understand that some of the circles are not plotting in the expected location. Which line(s) of your code is the first line that produces unexpected results? You can just copy-paste that line(s) so I can start looking around that area for the problem.
Vance Blake
on 26 Aug 2019
Ok, I have feedback for the first half of the code.
One reason this problem is a little bit difficult to trace is because the variable names are being changed, more than once. "HS_kept_x" is a reorganization of "HS_kept" which is a reorganization of "E" which is a reorganization of "keep_x2" which is just a subsample of "x" In order to track how the values are changing throughout the program, we must keep all 5 variables in mind since they are different organizations of the same data. It is much cleaner to just index "x" throughout rather than reassign subgroups of x to multiple variable names. Sometimes it's necessary to assign a new variable name to a subsample of data but doing that 5 times to the same data is unecessary complexity.
This section is unnecessary. You're just producing the 2nd output of sort().
% creates a matrix of the distances from min to max based on point number
for i = 1:n
indx(i)=find(distances==dist_order(i));
end
distance_indexnum = transpose(indx); % transposes indx matrix
% Instead, do this
[dist_order, distance_indexnum] = sort(distances(:,1), 'ascend');
% instead of
pnd = [dist_order, distance_indexnum];
% you could do
pnd = distances(distance_indexnum,:)
% Instead of
for i = 1:n
colpnd = pnd(i,2);
x2(i) = x(colpnd, 1);
y2(i) = y(colpnd, 1);
A(i,:) = [x2(i), y2(i)]; % new matrix of points
end
% Do
A = [x(pnd(:,2)), y(pnd(:,2))];
HS_gen1 = A; % Why change the variable name? This makes
% the code very difficult to read.
Now we get to this section which I don't understand. I'm fairly certain that this is where the error starts to occur. Could you explain what elim_dist1 is? For example, what are the columns and rows? How does elim_dist1(i,j) relate to x and y?
% HS-HS Gen 1 Elimination
elim_dist1 = nan(numel(x)); % places nans on the diagonal after distance calculation
HS_HS_threshold = 24;
for i = n:-1:1 % looping from largest index to avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix
for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once
elim_dist1(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
elim_dist1(j,i) = elim_dist1(i,j);
end
end
Vance Blake
on 26 Aug 2019
Edited: Vance Blake
on 26 Aug 2019
Adam Danz
on 26 Aug 2019
#1: ok, makes sense.
#2: it's vectorization combined with indexing. Here's a good resource: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
3: Ok, got it.
First, this block of code....
% HS-HS Gen 1 Elimination
elim_dist1 = nan(numel(x)); % places nans on the diagonal after distance calculation
HS_HS_threshold = 24;
for i = n:-1:1 % looping from largest index to avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix
for j = (i-1):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once
elim_dist1(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
elim_dist1(j,i) = elim_dist1(i,j);
end
end
...can be replaced with these 2 lines.
elim_dist1 = squareform(pdist([x,y]));
elim_dist1(1:size(elim_dist1,1)+1:end) = NaN;
Now let's look at this similar block of code. This is where the error is happening.
% Circle of influence elimination VN-HS test & isolates hormone seeds that fail condition
elim_dist2 = nan(numel(x)); % this to have nans on the diagonal after distance calculation
VN_HS_threshold = 16;
for i = LengthB:-1:1 % looping from largest index lets you avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix
for j = (i-2):-1:1 % distance symmetric (l(i1,i2) == l(i2,i1)) so calculate it once
elim_dist2(i,j) = sqrt((keep_x(i)-vn_x(j)).^2 + (keep_y(i)-vn_y(j)).^2);
elim_dist2(j,i) = elim_dist2(i,j);
end
end
% find the points that have its nearest neighbour further away than VN_HS_threshold:
i2keepVN_HS = find(min(elim_dist2)> VN_HS_threshold);
% put those into one pair of arrays
keep_x2 = x(i2keepVN_HS);
keep_y2 = y(i2keepVN_HS);
Have you looked at the values of elim_dist2? Here they are below, after both loops are complete.
elim_dist2 =
NaN NaN 57.516 58.335 NaN NaN NaN NaN NaN NaN
NaN NaN NaN 50.37 NaN NaN NaN NaN NaN NaN
57.516 NaN NaN NaN NaN NaN NaN NaN NaN NaN
58.335 50.37 NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
The i-loop only loops over 4 iterations because "LenghB" (bad variable name) equals 4. So you'll never get past the 4 row or column of elim_dist2 even though there are 10 rows and columns. The rest will always be NaNs.
Then you find which columns in elim_dist2 have values greater than 16 and of course that will never include columns 5:10 because they are all NaNs. Nevertheless, you use this index to select values of x and y which have 10 elements.
That makes no sense. So you probably want to re-think this section and what it's supposed to be doing.
Vance Blake
on 27 Aug 2019
"I only want it to keep the 4 HS points at that stage of the code. "
i2keepVN_HS is equal to [1,2,3,4] because columns 5:10 of elim_dist2 are always NaNs.
x and y are vectors and each of them have 10 elements. So, the two lines below will only choose the first 4 element of x and y and will always ignore the rest as if they don't even exist.
Is that really want you want to do, to always ignore elements 5 to 10 of x and y?
keep_x2 = x(i2keepVN_HS);
keep_y2 = y(i2keepVN_HS);
"keep_x2" becomes part of "E" which is renamed to "HS_kept" which is part of "HS_kept_x" (a nightmare of variable name changes) and HS_kept_x (which is the same as keep_x2) are the values that are creating circles in the unexpected areas. So I'm quite certain that's the source of your error. I think you should run the code up to the beginning of that section and step through each of those line, line-by-line, and think about what each variable is supposed to represent, what it's supposed to look like, the size, shape, and the values, in order to find the glitch.
Vance Blake
on 27 Aug 2019
Adam Danz
on 27 Aug 2019
"So I need to rewrite the second elimination test so that it is no longer working with the original x and y and instead use the chosen x and y that survived the first HS-HS elimination test."
&
"So if my understanding is correct the way the code is written now for the second test is going back to the original x and y instead of the surviving x and y coordinates stored in matrix B"
That sounds right! You know the code better than I and that sounds logical.
Vance Blake
on 27 Aug 2019
Vance Blake
on 28 Aug 2019
Edited: Vance Blake
on 28 Aug 2019
Adam Danz
on 28 Aug 2019
Going line-by-line is often the best way to troubleshoot code and to really understand what's happening in the code. Well done!
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB 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!