이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Deleting circles drawn using viscircles
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello, I am trying to delete a group of circles drawn on my plot using viscircles. I have saved the group to a handle, but when I try to execute the delete(handle) command the circles still remain. I am using 'hold on' to continuously update the same figure could that be interfering? The points that the circles surround are removed when I delete their handle. below is a picture of before and after I run the delete(handle) commands. Would usign data linking/sourcing be a better option?? Would showing the updates via a new subplot window also work ??
Before:
After:
댓글 수: 2
Adam
2019년 8월 19일
Deleting handles returned from viscircles works fine when I try it, but I don't know how you are using it specifically.
Vance Blake
2019년 8월 19일
편집: Vance Blake
2019년 8월 19일
I am using viscircles to place circles around the points on the plot. I have all the circles saved to a handle called CTR_circles1 but when i delete CTR_circles1 the circles don't disappear. the relevant parts of the code are shown below. I do this task many more times thorughout the code but for the initial time I want to use it, it doesn't work.
% plots circles centered around the randomly generated points
for i = 1:n
radii_node = 4;
centers_node = [x(i), y(i)];
CTR_circles1 = viscircles(centers_node, radii_node, 'color', 'r');
hold on
end
%%
delete(CTR_circles1);
채택된 답변
Adam Danz
2019년 8월 19일
편집: Adam Danz
2019년 8월 20일
The handles are being overwritten on each iteration of the i-loop so at the end of the loop, you only have access to the last drawn cirlcle(s).
Instead,
% plots circles centered around the randomly generated points
CTR_circles1 = gobjects(1,n); % or (n,1)
hold on
for i = 1:n
radii_node = 4;
centers_node = [x(i), y(i)];
CTR_circles1(i) = viscircles(centers_node, radii_node, 'color', 'r');
end
%%
delete(CTR_circles1);
Or better yet, use input matrices to avoid the loop.
centers_node = [x(:), y(:)];
radii_node = 4 * ones(size(centers_node,1),1);
CTR_circles1 = viscircles(centers_node, radii_node, 'color', 'r');
댓글 수: 24
Vance Blake
2019년 8월 20일
Unfortunately, the top solution where you use gobjects didnt work for me. What would using input matrices in my code change slash improve cause I regrettable dont understand the benefits of the second way ??
Adam Danz
2019년 8월 20일
편집: Adam Danz
2019년 8월 20일
"the top solution where you use gobjects didnt work for me"
I'm sure it would be an easy fix.
- Was there an error or an unexpected outcome?
- What version of matlab are you using?
"What would using input matrices in my code change slash improve..."
- "vectorization" is usually cleaner and faster than using loops.
- There's no need to pre-allocate any loop variables
- All of your circle handles are stored in one variable (same with loop if you set it up correctly).
- Most people find vectorization more readable.
- One execution of the function and all of its overhead instead of 'n' executions
- Reduces the number of lines of code
Vance Blake
2019년 8월 20일
Im using matlab r2019a and there was no unexpected error. I think it has something to do with me reading the data in from table stored in a text file b/c when I run the code thru for the second set of data points and try to delete those circles the same thing happens with howvever with only 1 circle (the last one drawn) being deleted instead of all one circle this time.
Adam Danz
2019년 8월 20일
I made one small correction to the loop method in my answer.
CTR_circles1 = gobjects(n); % Old version, incorrect
CTR_circles1 = gobjects(1,n); % or (n,1); New Version, correct
It sounds like it's not being implemented correctly. You may have to make some small adjustments for it to fit your needs.
Here are some things to check to make sure it's working properly.
- At the end of the i-loop, 'CTR_circles1' should be a vector of handles.
- n should be an integer and probably >1
- This loop should only be run once. If you're running it more than once (for example, if this loop is within another loop) then you're surely overwrite the values in 'CTR_circles1'
Vance Blake
2019년 8월 20일
편집: Vance Blake
2019년 8월 20일
Alrigh thank you for all the suggestions I will try these and get back to you.
edit: unfortunately now none of the circles are deleted. CTR_circles is a vector of graphics and not handle names. Is it that there is a problem deleting graphics that are tied to data stored in files ?? I have considered clearing the axes entirely, but I need to be able to run this loop a minimum of 5 times so i would have to erase and re graph the data points at least 4 times which is not ideal. Again thanks for all your help with this unusual problem. Also should i contact the developers if the issue persists ?
edit: Could the for loop be causing problems as well?
Adam Danz
2019년 8월 20일
편집: Adam Danz
2019년 8월 21일
CTR_circles1(n) = viscircles(centers_node, radii_node, 'color', 'r');
% ^ old, dumb typo!
CTR_circles1(i) = viscircles(centers_node, radii_node, 'color', 'r');
% ^ new & correct
Here's a complete demo to show that this works. Sorry about the typo.
n = 5;
x = 1:5;
y = 1:5;
CTR_circles1 = gobjects(1,n); % or (n,1)
hold on
for i = 1:n
radii_node = 4;
centers_node = [x(i), y(i)];
CTR_circles1(i) = viscircles(centers_node, radii_node, 'color', 'r');
end
Now delete them.....
delete(CTR_circles1);
The vectorization method was also corrected and I still want to recommend that version over the for-loop (if you want, give that another try with the corrected version).
Vance Blake
2019년 8월 21일
편집: Vance Blake
2019년 8월 21일
Hey Adam thank you so much for your commitment to figure this out. I really appreciate your help with this problem. You're well on your way to MVP status. If im understanding correctly attaching 'i' ties the handle to each iteration of the loop and thus allows complete deletion.
Vance Blake
2019년 9월 9일
Hey Adam so Ive run into a bit of a problem. when using the splitapply solution you gave me for sorting the HS points based on their closest vein node. I get the error
Error using splitapply (line 111)
For N groups, every integer between 1 and N must occur at least once in the vector of group
numbers.
Error in Constructal_Theory_Research_Simple_Algorithm_VER8 (line 809)
VN_HSNeighborGroups2 = splitapply(@(x){x},HS_kept,minRowIdx2(:));
From my understanding I have realized that not a single point is closest to to the second vein node so splitapply can't activate becasue not every integer value from 1-4 is represented (2 is omitted) are there any work arounds that will still split my points up properly because I cant continue on to the next stage of the code without splitting them into their groups?? Is the away to temporarliy remove vein node 2 and any other troublesome vein nodes in the future from the calculations??
Thank you for your help in this and all throughout today.
Adam Danz
2019년 9월 9일
Hi Vance, I'd be glad to suggest an alternative. Could you point me to the post where I suggested splitapply() because that's not mentioned in this post. I need to remember what the goal was with that line of code.
Vance Blake
2019년 9월 9일
편집: Vance Blake
2019년 9월 9일
Hi Adam it was actually this post where I set off a firestorm last night lolhttps://www.mathworks.com/matlabcentral/answers/478472-sorting-points-based-on-comparing-distances In the original code we only had 2 points so it was highly likely that both points would have nearby neighbors but now that I have 4 black vein nodes the second one isnt close to any of them them. A now has these 4 points and B has these 9 points. I was looking into the see also pages under splitapply on mathworks site but i dont think they are what i am looking for.
A= [0 0
-5.52181708330649 5.78874218621840
7.84112142353224 -1.58644723247378
-10.7646822498633 11.8312884082431]
B = [38.8197976955836 -29.0434116907097
-37.0532684880158 0.644925865563445
-4.49735986053992 57.3402937422674
-43.7442096431328 38.5935144262550
41.5359946082739 41.4696332098067
57.3572679057450 8.87552592324304
-29.8320366435934 -43.1286701525403
-12.7958017317891 -16.4459498330297
10.3504648148877 26.5921703744806]
Adam Danz
2019년 9월 9일
This was an easy one to solve. Use findgroups() to create the group input to splitapply().
The first output to findgroups() is the grouping variable, the 2nd output is the key. I'll let you read the documentation to understand more: https://www.mathworks.com/help/matlab/ref/findgroups.html
C = pdist2(A,B);
% or better: C = sqrt((B(:,1)-A(:,1)').^2 + (B(:,2)-A(:,2)').^2)';
% Split B into groups that are nearest to points in A
[~, minRowIdx] = min(C,[],1); %
[groupID, groupList] = findgroups(minRowIdx); % <------- HERE
neighborGroups = splitapply(@(x){x},B,groupID(:)); % <------- HERE
However, neighborGroups is now organized a bit differently. Before, neighborGroups{n} were all the coordinates in B that are closest to A(n,:).
Now, neighborGroups{n} are all the coordinates in B that are closest to A(groupList(n),:).
Vance Blake
2019년 9월 9일
Alright I saw someone on another forum suggest using findgroups but I didnt understand how to put it into practice. If my understanding is correct, then neighborgroups{n} is a matrix of the coordinates in B that are closest to A(grouplist(n), :) (n = 1:4) with grouplist creating n groups every time so that splitapply will always have all integers from 1 to n represented even if nothing is going into a certain n group right ??
Adam Danz
2019년 9월 9일
I think if you just look at the vector stored in 'groupList' it will make more sense.
groupList =
1 3 4
This means there are are no points that are closest to A(2,:) since '2' is missing from that vector.
Another example, when n=3, groupList(n) equals 4. So when you execute this line below you are looking at the 4th row of 'A' even through n=3.
A(grouplist(n), :)
Vance Blake
2019년 9월 9일
I was just about to test it out so that I could check i just wanted know if I was reading the findgroups page correctly. I thought that it'll always report the same number of groups but instead of doing that it only reports the groups relevant to the function. I'll check back in after I mess around with it for a bit. As usual thanks for your help! Ill make a quesiton out of this after it works as thanks and as an apology for what i put you thru yesterday. You should get credit for being so helpful.
Adam Danz
2019년 9월 9일
Ha!
I remember being in your situation, though for whatever reason I didn't get involved with this forum until much later. Once you learn how to look up stuff in the documentation and start to become familiar with the lingo, you'll find that your level of independent will sky rocket.
Vance Blake
2019년 9월 9일
편집: Vance Blake
2019년 9월 9일
As the cliche goes, "practice makes perfect" lol. But I tried it and it worked very swimmingly but now when I am trying to add my combined normalized vectors back to the point they were calculated from in A I get the "matrix dimensions must be equal error". I have placed the link to my quesiton below. I am trying to make it so that the code works whether it is adding equal size matrices if not,
Edit: Nvm syntax error found it going line by line after my 5th time thru the code
wrote A(groupList(i, :)) instead of A (groupList(i),:)
Adam Danz
2019년 9월 9일
편집: Adam Danz
2019년 9월 9일
The problem is
G(i,:) = F(i,:)+ A(groupList3(i,:));
should be
G(i,:) = F(i,:)+ A(groupList3(i),:);
% ^^^^^
Which you correctly implemented here
D{i,:} = neighborGroups3{i}-A(groupList3(i),:);
Lesson: when you get such an error, take a moment to look into the what's happening. "F(i,:)" produced a vector with 2 elements, "A(groupList3(i,:))" produced a vector with 3 elements even though A only has 2 columns. That should have been an indicator that something was wrong.
[update after seeing your comment above]
For future reference, you don't need to go line by line. The error message tells you the line number where the error is happening. Start there. Sometimes you do have to backtrack and go line by line but not in this case.
Vance Blake
2019년 9월 10일
True it does do that but I didn't realize the error until i saw the line above where it was done properly lol. But I do have another problem that I've been working on since I last responded. If you remeber the vectorized sorting that happens in my code. For some reason, Im trying to do it again at a later part stage but it keeps reporting a value that doesnt fail. The error seems to be happening with the i2keepVN_HS. I use this code multiple times throughout the code but here it is glitching and reporting both the vein node(j) index number and the HS(i) index number instead of just the HS index number. Ive looked at the code in question and compared it to the other part that use the same syntax and they are the same. can you spot the difference Ive posted the two sections below
% THIS SECTION WORKS
% Circle of influence elimination test between newest VN and all existig HS & isolates hormone seeds that fail condition
elim_dist7 = nan(numel(keep_x6));
VN_HS_threshold = 16;
for i = SizeofL:-1:1 % looping from largest index lets us avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix
for j = SizeNewVN_prime:-1:1
elim_dist7(i,j) = sqrt((keep_x6(i)-newVN_prime_x(j)).^2 + (keep_y6(i)-newVN_prime_y(j)).^2);
elim_dist7(j,i) = elim_dist7(i,j);
end
end
% find the points that have its nearest neighbour further away than VN_HS_threshold:
i2keepVN_HS = find(min(elim_dist7)> VN_HS_threshold);
% put those into one pair of arrays
keep_x7 = keep_x6(i2keepVN_HS);
keep_y7 = keep_y6(i2keepVN_HS);
% and the others into another pair of arrays
x_close_neighbors7 = keep_x6;
y_close_neighbors7 = keep_y6;
x_close_neighbors7(i2keepVN_HS) = [];
y_close_neighbors7(i2keepVN_HS) = [];
M = [keep_x7, keep_y7];
HS_kept = M;
However
% THIS SECTION DOES NOT
elim_dist10 = nan(numel(keep_x10));
VN_HS_threshold = 16;
for i = SizeofO:-1:1 % looping from largest index lets us avoid calculating the size of the elim_dist2 matrix without pying the price of dynamic growing of a matrix
for j = SizeNewVN_prime2:-1:1
elim_dist10(i,j) = sqrt((keep_x10(i)-newVN_prime_x2(j)).^2 + (keep_y10(i)-newVN_prime_y2(j)).^2);
elim_dist10(j,i) = elim_dist10(i,j);
end
end
% find the points that have its nearest neighbour further away than VN_HS_threshold:
i2keepVN_HS2 = find(min(elim_dist10)> VN_HS_threshold);
% put those into one pair of arrays
keep_x11 = keep_x10(i2keepVN_HS2);
keep_y11 = keep_y10(i2keepVN_HS2);
% and the others into another pair of arrays
x_close_neighbors11 = keep_x10;
y_close_neighbors11 = keep_y10;
x_close_neighbors11(i2keepVN_HS2) = [];
y_close_neighbors11(i2keepVN_HS2) = [];
P = [keep_x11, keep_y11];
HS_kept = P;
Vance Blake
2019년 9월 10일
편집: Vance Blake
2019년 9월 11일
Thank you I will try it and get back to you!
edit: Hey Adam I figured out the problem. The reason the second part of the code wasn't working was because of the improper syntax of these two lines.
elim_dist10(i,j) = sqrt((keep_x10(i)-newVN_prime_x2(j)).^2 + (keep_y10(i)-newVN_prime_y2(j)).^2);
elim_dist10(j,i) = elim_dist10(i,j);
I discovered that I don't need the second line for the vein node to HS exclusions because it just confuses the i2keep lines by equating VN j numbers and HS i numbers. and the first line of code needs to be
elim_dist(j,i) = %sqrt---other stuff--
so that when searching the elim_dist matrix it only looks at the i values. the resason the HS_HS elim test worked with that syntax was due to them always being symmetric. So it wouldn't matter whether i or j was being reported to the i2keep line. Thanks for your help again!
Vance Blake
2019년 9월 13일
Hey Adam, Happy Friday! Just wondering but I wanted to put annotations/text boxes at specific coordinates but when I read up on the page I saw that the values of x and y in the 'dim' vector must be between 0 and 1. Im just looking for a way to place 'X's at specific xy coordinates to signify that those points are no longer being considered for my calculations?? I figured annotations would be good way to accomplish that because I wanted to avoid matlab cconsidering a plotted X as an active point and therefore valid for my calculations. If you have any work arounds or suggestions Im all ears. Have a great weekend!
Adam Danz
2019년 9월 13일
How about we start a new question since we've diverged a bit from the original topic which may make the thread difficult to follow?
Vance Blake
2019년 9월 13일
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)