Plot cell array as a histogram using hist3?

4 views (last 30 days)
I have a 30 x 30 cell array and I want to visualize the data in the cell array on a 30 x 30 Bivariate histogram plot using hist3. Each cell in the cell array should represent a grid on the plot & its location on the plot should be the same as its location in the cell array. The data values should be represented on the z axis. Is there any way to achieve this on Matlab?

Answers (1)

Akira Agata
Akira Agata on 14 Jun 2017
You can use bar3 to do this. Here is an example.
% 30x30 cell array sample
C = num2cell(randi(100,30));
% Bivariate histogram
bar3(cell2mat(C));
If you want to change the color of each bar according to its value, please try this.
% Bivariate histogram (color represents each value)
h = bar3(cell2mat(C));
for kk = 1:numel(h)
h(kk).CData = h(kk).ZData;
h(kk).FaceColor = 'interp';
end
  2 Comments
Atiqah Zakirah
Atiqah Zakirah on 15 Jun 2017
Thank you for the reply! However, I received this error "Dimensions of matrices being concatenated are not consistent". The images attached are a snippet of my 30 x 30 cell array and the values in each cell. Each cell holds 24 values and all cells hold different values from each other. I want to plot the each row value of each cell on a 3D histogram. That means I should have a total of 24 graphs. Can bar3 still be used to achieve this?
Akira Agata
Akira Agata on 15 Jun 2017
OK. Then, how about this? The following code generates 24 figures.
% 30x30 cell array sample (some cells have 24x1 numeric array)
C = cell(30);
C{2,2} = randi(100,24,1);
C{4,5} = randi(100,24,1);
for kk1 = 1:24
D = zeros(30);
for kk2 = 1:numel(C)
if isempty(C{kk2})
D(kk2) = 0;
else
D(kk2) = C{kk2}(kk1);
end
end
% Bivariate histogram (color represents each value)
figure
h = bar3(D);
for kk3 = 1:numel(h)
h(kk3).CData = h(kk3).ZData;
h(kk3).FaceColor = 'interp';
end
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!