簡體   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