Coloured circles on a mesh

2 次查看(过去 30 天)
I managed to get two coloured boxes on the mesh
I am struggling to imagine how I could get the circles with three different colours on this mesh
Any suggestions are most welcome.

采纳的回答

Andrew
Andrew 2025-6-9
编辑:Walter Roberson 2025-6-9
Hi
Here is the code I used to get the coloured blocks. I get my data from a simulation I run on MicroCap12.
clear
clc
writerObj = VideoWriter('C:\Users\WINDOWS11\Documents\MATLAB\EExp100100','MPEG-4');%Name of video file
open(writerObj);
fig1 = figure(1);
load EExp100100.TNO %Read in the file generated by Microcap 12 -No headers
d1 = EExp100100;
timestamp = d1(:,1);
d1(:,1)=[];%Remove time
rws=100; %Get this number from the Microcap 12 netlist
cols=100; %Get this number from the Microcap 12 netlist
s=size(d1);
d1ct=1;
%I use the same block coordinates I used to set up the MicroCap12 simulation
ind1a = (20:80);
ind1b = (1:40);
Z(ind1a,ind1b) = 0;
ind2a = (20:80);
ind2b = (60:100);
Z(ind2a,ind2b) = 0;
for k = 1:s(1)
for j=1:rws
for i=1:cols
Z(j,i)=d1(k,d1ct);
d1ct=d1ct+1;
end
end
d1ct=1;
x=1:1:rws;
xx=1:1:rws;%put in an increment for spline command
for p=1:cols
y=Z(:,p);
yy(:,p)=spline(x,y,xx);
end
syy=size(yy);
x=1:1:cols;
xx=1:1:cols; %put in an increment for spline command
for p=1:syy(1)
y=yy(p,:);
Zz(p,:)=spline(x,y,xx);
end
%s2=size(Zz);
%mztb=zeros(1,s2(2)+2);
%sidez=zeros(s2(1),1);
%Zc=[mztb;sidez Zz sidez;mztb];%put some 0's around the data
%[X,Y] = meshgrid(1:1:s2(2)+2, 1:1:s2(1)+2);
[X,Y] = meshgrid(1:1:100, 1:1:100);
%mesh(X,Y,Zc);
mesh(X,Y,Zz)
colormap(bone)
%colormap(sky)
view([-50 70])
title('100 by 100 grid (spline = 1)')
xlabel('X')
ylabel('Y')
txt = ['Time: ' num2str(timestamp(k,1)) ' secs'];
text(-15,-5,txt)
%axis([1 s2(2)+2 1 s2(1)+2 -2 2])
axis([1 100 1 100 -2 2])
hold on
m1 = mesh(X(ind1a,ind1b),Y(ind1a,ind1b),Z(ind1a,ind1b));
set(m1,'Facecolor','r');
m2 = mesh(X(ind2a,ind2b),Y(ind2a,ind2b),Z(ind2a,ind2b));
set(m2,'Facecolor','r');
view(-50,70)
hold off
F=getframe(fig1);
%write the current frame to the writer object
writeVideo(writerObj,F)
end
%Close the writer object. File appers in current folder
close(writerObj);
disp('Video File Written')

更多回答(1 个)

Benjamin Kraus
Benjamin Kraus 2025-6-9
编辑:Benjamin Kraus 2025-6-9
What approach did you use to draw the rectangles?
The easiest approach to drawing circles in MATLAB is using the rectangle command and setting the Curvature to 1. This can only be used to draw circles in the X/Y plane, which looks (from your picture) like what you are trying to do.
rectangle('Position',[0 0 1 1],'Curvature',1,'FaceColor','green')
view(3)
grid on
box on
If you need circles in another plane, you have two options:
Option 1a: Use an hgtransform to rotate your circle to the correct plane.
figure
h = hgtransform('Matrix',makehgtform('xrotate',pi/2));
rectangle(h,'Position',[0 0 1 1],'Curvature',1,'FaceColor','green')
view(3)
grid on
box on
Option 1b: You can also use an hgtransform to translate your circle if you need it somewhere other than at Z=0.
figure
h = hgtransform('Matrix',makehgtform('translate',[0 0 1]));
rectangle(h,'Position',[0 0 1 1],'Curvature',1,'FaceColor','green')
view(3)
grid on
box on
Option 2: Use a patch (the patch or fill3 commands) to draw your circle in an arbitrary dimension.
figure
t = linspace(0,2*pi,361);
x = zeros(size(t));
y = cos(t);
z = sin(t);
fill3(x,y,z,'g')
view(3)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by