How to obtain histogramof each individual matrix in cell array?

I have a 8 by 8 cell and each cell consist of a matrix with 1 row and 49 columns.how do i compute histogram for each cell using a for loop?
I would like to plot for each matrix in the cell array and meaning i will obtain 64 figures of histograms as i have 64 cells. How do I access each matrix to plot the histogram?

 Accepted Answer

In its simplest form:
cellfun(@histogram, yourcellarray);
If you want to specify the number of bins or edges
nbins = 5;
cellfun(@(c) histogram(c, nbins), yourcellarray);

8 Comments

Sorry if my question is unclear.
I would like to plot for each matrix in the cell array and meaning i will obtain 64 figures of histograms as i have 64 cells. How do I access each matrix to plot the histogram?
Thanks for your help
That is what Guillaume's code does. Did you try it?
Well, it is what my code do, but not what I intended actually. I meant to use histcounts and not histogram. But, yes, my code answers the question exactly.
I've just the superfluous 'UniformOutput', since there's actually no output.
@Kimberly, you can write exactly the same thing with a loop, the first cellfun is exactly equivalent to:
for celliter = 1:numel(yourcellarray)
histogram(yourcellarray{celliter});
end
Yup..I tried. I have an error saying Undefined function or method 'histogram' for input arguments of type 'uint8'.
I am sorry as I am new in matlab. Thanks for your kind explanation
You must be using an old version of matlab (pre r2014b).
Replace histogram by hist.
I am using r2010a.
after using cellfun(@hist,totalblocks1);
I get an error writing:
??? Error using ==> times Integers can only be combined with integers of the same class, or scalar doubles.
Error in ==> hist at 78 xx = miny + binwidth*(0:x);
Error in ==> imageblocksmatrix at 264 cellfun(@hist,totalblocks1);
This latest error occurs because your matrices are of type uint8 instead of double and old versions of matlab weren't good at coping with non-double numerics. A simple way to fix this:
cellfun(@(c) hist(double(c)), totalblocks1);
The same, using a loop:
for celliter = 1:numel(totalblocks1)
hist(double(totalblocks1{celliter}));
end
I would recommend you move to a newer version of matlab, if possible. A lot has been improved since 2010.
It is working now. Thanks alot for your help!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 15 Dec 2015

Commented:

on 16 Dec 2015

Community Treasure Hunt

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

Start Hunting!