简体   繁体   中英

Matlab: Color Coding a 3D Plot

I've been trying to look around online for something I want but I'm not having much luck so I thought I would just ask on here.

Is it possible to pinpoint in a different color and show the point on the graph where there are intersections between the two plots?

Thanks for any help you can give.

Here is the code:

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

Changing the colours is simple, that's just a case of adding a colour code to the plot3 command, eg:

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

For more details on colour codes, see the Matlab Colourspec Page .

The intersection could be a bit more tricky, depending on whether you want the intersection of the points (ie specific points which appear in all 3 datasets) or the intersection points of the lines that join the points.

I think the former should be fairly easy (this is untested and assumes CX1 ,etc are vertical vectors):

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');

The intersection works like so:

Say we have 3 matrices, each containing a number of 3d points. Let's call them A , B and C .

To find the intersection between all 3 sets we first find the points that intersect just in A and B . We now have a set of points that we know are in A and B , so now we just have to check if those points are in C as well. we do this by doing another intersection.

I just chained those together into one line of code, which was probably not very useful, so I apologise. The code for the A , B , C intersections is below:

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.

We can then substitute D into the line E = ... and we get:

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

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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