matlab怎么将指定的三维坐标点连接起来

img

img


为什么matlab的gplot函数画出来的函数是二维的,但是这里的XY明明是三维的,有什么函数或方法可以画出三维的这样指定点连接的图形吗?

可以下载一下gplot3函数,地址如下:
https://ww2.mathworks.cn/matlabcentral/fileexchange/49762-gplot3-plotting-simple-graphs-in-3d
一个测试代码:

clc
clear
[D,XY] = bucky;
gplot3(D,XY,'-*')
axis square
function h = gplot3(A, xyz, varargin)
%GPLOT Plot graph (nodes and edges).
%   GPLOT(A, xyz) plots the graph specified by the adjacency matrix,
%   A, and the n-by-3 coordinate array, xyz.
%   
%   GPLOT(A, xyz, linespec) uses line type and color specified in the
%   string LineSpec. See PLOT for possibilities.
%
%   h = GPLOT(A, xyz) returns the a handle to the graph.
%   
%   h = GPLOT(A, xyz, 'LineWidth', 5, ...) also takes arbitrary arguments
%   for line properties

    % If no arguments given, then run buckminster sphere example
    if nargin == 0
        [A, xyz] = bucky;
    end

    % If only one argument given, throw error.
    if nargin == 1
        error('Please provide an adjacency matrix and coordinate array');
    end

    % Returns i and j, lists of connected nodes
    [i,j] = find(A);

    % Extact 
    X = [ xyz(i,1) xyz(j,1)]';
    Y = [ xyz(i,2) xyz(j,2)]';
    Z = [ xyz(i,3) xyz(j,3)]';

    % Add NaN values to break between line segments
    X = [X; NaN(size(i))'];
    Y = [Y; NaN(size(i))'];
    Z = [Z; NaN(size(i))'];

    % Serialize the x and y data
    X = X(:);
    Y = Y(:);
    Z = Z(:);
    
    % If only two arguments, then plot as is
    if nargin == 0 || nargin == 2
        h = plot3(X, Y, Z);
    end
    
    % If linespec given, then use it
    if nargin >= 3
        if mod(nargin, 2) == 1
            h = plot3(X, Y, Z, varargin{1});
            start = 2;
        else
            h = plot3(X, Y, Z);
            start = 1;
        end
        
        % Now apply the rest of the var string
        if ~isempty(varargin)
            for i=start:2:length(varargin)
                set(h, varargin{i}, varargin{i+1});
            end
        end
        
    end
    
end


运行结果:

img

码字不易,有用希望采纳一下哦

【以下回答由 GPT 生成】

首先,我们可以使用plot3函数来绘制三维坐标点的图形。然后,可以使用line函数在指定的点之间绘制连接线段。下面是一个示例代码,展示了如何使用plot3line函数来实现这个功能:

% 创建三维坐标点
x = [1, 2, 3, 4];   % x坐标
y = [5, 6, 7, 8];   % y坐标
z = [9, 10, 11, 12];   % z坐标

% 绘制三维坐标点
figure;
plot3(x, y, z, 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
hold on;

% 连接指定的三维坐标点
for i = 1:length(x)-1
    line([x(i), x(i+1)], [y(i), y(i+1)], [z(i), z(i+1)], 'LineWidth', 2, 'Color', 'b');
end
hold off;

在这个示例中,我们首先创建了三个分别表示x坐标、y坐标和z坐标的向量。然后,使用plot3函数将这些坐标点绘制出来,其中'o'表示使用圆圈形状的标记点。接下来,使用line函数在每两个相邻的坐标点之间绘制线段,从而连接起来。LineWidth参数设置线宽,Color参数设置线段的颜色。

希望这个示例能够解决你的问题。如果有任何问题,请随时提问。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^