繁体   English   中英

我希望能够在 MATLAB 的 AppDesigner 中 plot 一个 3D 圆圈。 我该怎么做?

[英]I want to be able to plot a 3D circle in MATLAB's AppDesigner. How can I do so?

我有一个代码,我将x1y1z1和半径 d 作为用户的输入。 在我的回调 function 中,我正在读取这些值,并且我必须在 MATLAB 的应用程序设计器中 plot 一个 3D 圆圈。 我该怎么做? 我有一个用于在 2D 中绘制一个点的代码,但是当我尝试使用 3D plot 时,同样的事情不起作用。

        x1 = app.NumericEditField.Value;
        y1 = app.NumericEditField4.Value;
        z1 = app.NumericEditField7.Value;
        
      
        plot(app.UIAxes,x1,y1,'o'); %Code for a point in 2D plot.
        grid(app.UIAxes,'on');

在 UIAxes 上绘制圆和球体

可以使用plot3() function 在 MATLAB 应用程序设计器中绘制一个圆圈。 然后可以使用 Sin() 和 cos() 获取一组 (x,y) 点,这些点可以存储在矩阵中。 还必须创建另一个存储 (z) 点的相同大小的矩阵。

简而言之,需要 x、y、z 点和 plot3() 的 3 个向量。 为了以防万一,我还包括了一个球体。

3D 圆图 3D 球体图 3D 球面图冲浪

app = uifigure();
app.Color = '#FFFFFF';

Field_Height = 20;
Field_Width = 80;

%Creating input text field for x1%
x1 = uieditfield(app,'numeric');
x1.Position = [10 100 Field_Width Field_Height];
x1_Label = uilabel(app);
x1_Label.Text = 'x1';
x1_Label.Position = [100 100 Field_Width Field_Height];

%Creating input text field for y1%
y1 = uieditfield(app,'numeric');
y1.Position = [10 70 Field_Width Field_Height];
y1_Label = uilabel(app);
y1_Label.Text = 'y1';
y1_Label.Position = [100 70 Field_Width Field_Height];

%Creating input text field for z1%
z1 = uieditfield(app,'numeric');
z1.Position = [10 40 Field_Width Field_Height];
z1_Label = uilabel(app);
z1_Label.Text = 'z1';
z1_Label.Position = [100 40 Field_Width Field_Height];

%Creating input text field for the radius%
Radius = uieditfield(app,'numeric');
Radius.Position = [10 10 Field_Width Field_Height];
Radius_Label = uilabel(app);
Radius_Label.Text = 'Radius';
Radius_Label.Position = [100 10 Field_Width Field_Height];

%Creating 3D axes%
Axes_3D = uiaxes(app);
Axes_3D.Position =   [150 20 400 400];
plot3(Axes_3D,0,0,0,'Color','b');
Axes_3D.XGrid = 'on';
Axes_3D.YGrid = 'on';
Axes_3D.ZGrid = 'on';
Axes_3D.XLabel.String = "x axis";
Axes_3D.YLabel.String = "y axis";
Axes_3D.ZLabel.String = "z axis";
Axes_3D.BackgroundColor = '#FFFFFF';

%Setting callback functions for each input field%
x1.ValueChangedFcn = @(x1,event) Plot_Sphere(x1,y1,z1,Radius,Axes_3D);
y1.ValueChangedFcn = @(y1,event) Plot_Sphere(x1,y1,z1,Radius,Axes_3D);
z1.ValueChangedFcn = @(z1,event) Plot_Sphere(x1,y1,z1,Radius,Axes_3D);
Radius.ValueChangedFcn = @(Radius,event) Plot_Sphere(x1,y1,z1,Radius,Axes_3D);

%Function that gets called when any input field is changed%
function [] = Plot_Sphere(X_Field,Y_Field,Z_Field,Radius_Field,Axes_3D)

%Grab the positions.offsets from each field%
X_Position = X_Field.Value;
Y_Position = Y_Field.Value;
Z_Position = Z_Field.Value;
Radius = Radius_Field.Value;

%Creating a matrix of points for the sphere%
[X_Base,Y_Base,Z_Base] = sphere;

%Multiplying the points depending on a given radius%
X = X_Base * Radius;
Y = Y_Base * Radius;
Z = Z_Base * Radius;

%Creating a matrix of points for the circle%
Number_Of_Data_Points = 200;
theta = linspace(0,2*pi,Number_Of_Data_Points);
X_Circle = Radius*cos(theta);
Y_Circle = Radius*sin(theta);
Z_Circle = zeros(1,Number_Of_Data_Points);

%Plotting the circle%
plot3(Axes_3D,X_Circle+X_Position,Y_Circle+Y_Position,Z_Circle+Z_Position);

%Switch this line to get sphere%
% plot3(Axes_3D,X+X_Position,Y+Y_Position,Z+Z_Position,'Color',[0, 0.4470, 0.7410]);
%Switch this line to get sphere filled%
% surf(Axes_3D,X+X_Position,Y+Y_Position,Z+Z_Position);

end

使用 MATLAB R2019b 运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM