조회 수: 22(최근 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:

Adam
19 Aug 2019

Vance Blake
19 Aug 2019

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
19 Aug 2019

편집: Adam Danz
20 Aug 2019

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');

Vance Blake
20 Aug 2019

Adam Danz
20 Aug 2019

"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
20 Aug 2019

Adam Danz
20 Aug 2019

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
20 Aug 2019

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
20 Aug 2019

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
21 Aug 2019

Vance Blake
9 Sep 2019

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
9 Sep 2019

Vance Blake
9 Sep 2019

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
9 Sep 2019

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
9 Sep 2019

Adam Danz
9 Sep 2019

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
9 Sep 2019

Adam Danz
9 Sep 2019

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
9 Sep 2019

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
9 Sep 2019

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
10 Sep 2019

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;

Adam Danz
10 Sep 2019

Vance Blake
10 Sep 2019

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
13 Sep 2019

Adam Danz
13 Sep 2019

Vance Blake
13 Sep 2019

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

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

Start Hunting!페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.

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: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- 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)