Matlab:颜色编码3D图

时间:2012-05-30 13:32:53

标签: matlab colors 3d

我一直试图在网上寻找我想要的东西,但我没有太多运气所以我想我会问这里。

是否可以精确定位不同颜色并在图表上显示两个图之间有交叉点的点?

感谢您提供任何帮助。

以下是代码:

file1 = fopen('C:\Program Files (x86)\Notepad++\avatar1.txt'); % open text file
file2 = fopen('C:\Program Files (x86)\Notepad++\avatar2.txt'); % open text file
file3 = fopen('C:\Program Files (x86)\Notepad++\avatar3.txt'); % open text file

tline1 = fgetl(file1); % read line by line and remove new line characters
tline2 = fgetl(file2); % read line by line and remove new line characters
tline3 = fgetl(file3); % read line by line and remove new line characters

% declare empty arrays
CX1 = [];
CY1 = [];
CZ1 = [];

CX2 = [];
CY2 = [];
CZ2 = [];

CX3 = [];
CY3 = [];
CZ3 = [];


while ischar(tline1) % true if tline is a character array
    temp = cell2mat(textscan(tline1, '<%n,%n,%n>'));

    % convert all the cell fields to a matrix
    CX1 = vertcat(CX1, temp(1));
    CY1 = vertcat(CY1, temp(2));
    CZ1 = vertcat(CZ1, temp(3));

    tline1 = fgetl(file1);
end

while ischar(tline2) % true if tline is a character array
    temp = cell2mat(textscan(tline2, '<%n,%n,%n>'));

    % convert all the cell fields to a matrix
    CX2 = vertcat(CX2, temp(1));
    CY2 = vertcat(CY2, temp(2));
    CZ2 = vertcat(CZ2, temp(3));

    tline2 = fgetl(file2);
end

while ischar(tline3) % true if tline is a character array
    temp = cell2mat(textscan(tline3, '<%n,%n,%n>'));

    % convert all the cell fields to a matrix
    CX3 = vertcat(CX3, temp(1));
    CY3 = vertcat(CY3, temp(2));
    CZ3 = vertcat(CZ3, temp(3));

    tline3 = fgetl(file3);
end

fclose(file1); % close the file
fclose(file2); % close the file
fclose(file3); % close the file

plot3(CX1, CY1, CZ1) % plot the data and label the axises
plot3(CX2, CY2, CZ2)
plot3(CX3, CY3, CZ3)
xlabel('x')
ylabel('y')
zlabel('z') 
grid on
axis square
rotate3d on; % activate interactive mouse rotation

1 个答案:

答案 0 :(得分:0)

更改颜色很简单,只是在plot3命令中添加颜色代码,例如:

plot3(CX1, CY1, CZ1, 'b'); % blue lines/markers
plot3(CX2, CY2, CZ2, 'r'); % red lines/markers
plot3(CX3, CY3, CZ3, 'g'); % green lines/markers

有关颜色代码的详细信息,请参阅Matlab Colourspec Page

根据您是否想要点的交点(即出现在所有3个数据集中的特定点)或连接点的线的交点,交点可能会有点棘手。

我认为前者应该相当容易(这是未经测试的,假定CX1等是垂直向量):

figure; % Open up a new figure
hold on; % This means the everything you plot stays in the figure and is not overwritten

% Plot the original points
plot3(CX1, CY1, CZ1, '-*b'); % blue lines/markers
plot3(CX2, CY2, CZ2, '-*r'); % red lines/markers
plot3(CX3, CY3, CZ3, '-*g'); % green lines/markers

% turn those 1xn vectors into 3xn matrices for each set of points
points1 = [CX1, CY1, CZ1];
points2 = [CX2, CY2, CZ2];
points3 = [CX3, CY3, CZ3];

% Find the intersection of the 3 sets
CX_intersect = intersect( points1, intersect( points2, points3, 'rows'), 'rows');

% Draw a scatter plot of the intersection points. the 'mo' means:
% m: magenta in colour, o: circular markers
scatter3( CX_intersect(:,1),CX_intersect(:,2),CX_intersect(:,3),'mo');

交叉路口的工作原理如下:

假设我们有3个矩阵,每个矩阵包含许多3d点。我们称他们为ABC

要查找所有3个集合之间的交集,我们首先找到仅在AB中相交的点。我们现在有一组我们知道位于AB的点,所以现在我们只需检查这些点是否也在C中。我们通过做另一个交集来做到这一点

我只是把它们链接成一行代码,这可能不是很有用,所以我道歉。 ABC交叉点的代码如下:

D = intersect(A,B,'rows') % we use rows because each row represents a 3D point
E = intersect(C,D,'rows') % E is the intersection of the 3 sets.

然后我们可以将D替换为E = ...行,我们得到:

E = intersect( intersect(A,B,'rows'), C, 'rows' );

希望有所帮助!