change the color nodes

5 views (last 30 days)
farid khalafi
farid khalafi on 5 Jun 2019
Commented: farid khalafi on 5 Jun 2019
hi
i want change the color some nodes like the this pic but i can't
pls help me
thanks
  2 Comments
Adam Danz
Adam Danz on 5 Jun 2019
OP's code in the attached nodes.m file:
clc
clear
n = 10;
m=10;
b=2;
l=2;
x = linspace(0,b,n+1);
dx = x(2)-x(1);
y = linspace(0,l,m+1);
dy =y(2)-y(1);
X=[0:dx:b];
Y=[0:dy:l];
hold on
for i=1:m+1
for j=1:n+1
xe=X(i);
ye=Y(j);
plot(xe,ye,'rx','LineWidth',5,'MarkerSize',2,'color','b')
end
end
title('Nodes')
Steven Lord
Steven Lord on 5 Jun 2019
What function did you use to create this plot? That will affect how or if you can do what you're asking.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 5 Jun 2019
Edited: Adam Danz on 5 Jun 2019
Your code is plotting each individual point within two for-loops. One option is to specify the color of the nodes within the for-loops.
However, if you use gscatter() you can plot all of the nodes at once (without for-loops) and each node will still have it's own unique handle which will allow you to change the color of individual nodes. If you already know the color grouping, an even better solution would be to use the grouping variable (3rd input to gscatter) to group the nodes by color.
% Part of your code
n = 10;
m=10;
b=2;
l=2;
x = linspace(0,b,n+1);
dx = x(2)-x(1);
y = linspace(0,l,m+1);
dy =y(2)-y(1);
X=0:dx:b;
Y=0:dy:l;
% Create the grid of nodes
[xmat,ymat] = meshgrid(X,Y);
group = 1:numel(xmat);
figure();
h = gscatter(xmat(:),ymat(:),group',[0 0 1],'x',6);
set(h,'LineWidth', 4)
% remove auto-generated legend
delete(findobj(h(1).Parent.Parent, 'type','legend'));
Now you can change the color of individual nodes in two ways. First way: If this is a once-and-done action, perhaps the best way would be to select the white arrow icon in the tool menu at the top of the figure, right-click the code, and select "color" from the menu so you can select any color. Alternatively, you could left-click the node to make that object active and from the command window,
set(gco, 'Color', 'g')
Second way : from within the script or function, 'h', is a vector of handles for each node. You can find the nodes you want to change and then set their color like this:
nodeIndex = [10:20];
set(h(nodeIndex), 'Color', 'g')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!