This question links to my previous post.
Consider the below code:
%% How to plot each matrix in a cell in 3d plot(1 matrix with 1 color) ?
% Generate Sample data cell A(1x10 cell array)
clear; clc;
A = cell(1,10); % cell A(1x10 cell array)
for kk = 1:numel(A)
z = 10*rand()+(0:pi/50:10*rand()*pi)';
x = 10*rand()*sin(z);
y = 10*rand()*cos(z);
A{kk} = [x,y,z];
end
% Plot point of each matrix in one figure with different color
figure
hold on;
for i = 1:numel(A)%run i from 1 to length A
C = repmat([i],size(A{i},1),1);%create color matrix C
scatter3(A{i}(:,1),A{i}(:,2),A{i}(:,3),C,'filled');
end
grid on;
view(3); % view in 3d plane
colorbar;
This is the image result from the above code:
MY QUESTION:
If I want to use "color map" to show the color corresponding to the number of matrices, how can that be done?
Example: In the posted code, I have 10 matrices (A{1}
, A{2}
, A{3}
,..., A{10}
) inside the cell A
, so how to make the colorbar show the 10 colors used in the plot and how to show 10 numbers from 1 to 10 corresponding to the 10 colors used in the plot (as shown in the image)?
In the following lines of your code,
C = repmat([i],size(A{i},1),1);%create color matrix C
scatter3(A{i}(:,1),A{i}(:,2),A{i}(:,3),C,'filled');
The fourth input argument of scatter3
, which you named C
, does not specify color. It is for specifying the size of the circle being plotted. Just because you named it C
, MATLAB would not automatically recognise that you meant color. You're getting different colors because you're plotting multiple points with hold on
.
Coming towards your actual question and building from my previous answer,
newA = vertcat(A{:}); %Concatenating all matrices inside A vertically
numcolors = numel(A); %Number of matrices equals number of colors
colourRGB = jet(numcolors); %Generating colours to be used using jet colormap
colourtimes = cellfun(@(x) size(x,1),A);%Determining num of times each colour will be used
colourind = zeros(size(newA,1),1); %Zero matrix with length equals num of points
colourind([1 cumsum(colourtimes(1:end-1))+1]) = 1;
colourind = cumsum(colourind); %Linear indices of colours for newA
scatter3(newA(:,1), newA(:,2), newA(:,3), [] , colourRGB(colourind,:),'filled');
%However if you want to specify the size of the circles as well as in your
%original question which you mistakenly wrote for color, use the following line instead:
% scatter3(newA(:,1), newA(:,2), newA(:,3), colourind , colourRGB(colourind,:),'filled');
grid on;
view(3); %view in 3d plane
colormap(colourRGB); %using the custom colormap of the colors we used
%Adjusting the position of the colorbar ticks
caxis([1 numcolors]);
colorbar('YTick',[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],...
'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]);
which gives the following result:
If you want to change the size of the circles as you were mistakenly doing in your code, use the relevant line for plotting which is mentioned in the code. Using that generates the following result: