Is there a way to extracted histogram data into a matrix instead of graph?

Hello,
Is there a way to extract histogram data into a matrix instead of a graph?
for mer= start:Add:N_Mer
FileName_1 = ['Info_',num2str(N_Mer),'.mat'];
disp([FileName_1,' Running'])
M=load(FileName_1);
all_rows=cell2mat(struct2cell(M));
R_sqr{N_Mer}=all_rows(1,:);
FileName=(['hist_R2',num2str(N_Mer)]);
hist_R2{N_Mer}=histogram(R_sqr{N_Mer});
legend
hold on
end

 Accepted Answer

If you want to make a plot and also extract the values
% some data
foo = randn(10000,1);
% make a histogram named h
h = histogram(foo);
% get the values of the histogram
binedges = h.BinEdges;
binwidth = h.BinWidth;
bincounts = h.Values;
Alternatively, if you don't want a plot, but just want to extract histogram information
% some data
foo = randn(10000,1);
% extract histogram data
[bincounts, binedges] = histcounts(foo);

7 Comments

It's a little strange that bincount is 1 element smallaer than binedges. It should be the same as it count the number of repetion between the min and the max. Spisially the min here (0) and is repeated several time and the max (64) is repeated 4 times. Why are the legth of bincount and binedges is different then?
They are the edges of the bins.
If you want the centres of the bins then do:
bincentres = binedges(1:end-1)+diff(binedges)/2;
It seems that the function doesn't do a good job with larg data above 43,000. I see that there are repeated numbers between 0 ans 64 but it shows only 64 elements instead of 65.
Attach the data that proves that so we can try it ourselves.
The bin counts are fence rails/sections, the bin edges are the fence posts. See the Wikipedia page for "off by one error".
Ten bins would require eleven bin edges as shown in the picture in the "Fencepost error" section on that Wikipedia page.
Check this code with the below file please.
clc % To clear the screen
clear % To clear the Memory
t = tic;
time = [];
start = 8;
Add = 1;
Num_Mon = 8; % Total number of steps
for Num_monomers= start:Add:Num_Mon
FileName_1 = ['Info_',num2str(Num_monomers),'.mat'];
disp([FileName_1,' Running'])
M=load(FileName_1);
all_rows=cell2mat(struct2cell(M));
R_sqr=all_rows(1,:);
FileName=(['hist_R2',num2str(Num_monomers)]);
[bincounts, binedges] = histcounts(R_sqr);
bincentres = binedges(1:end-1)+diff(binedges)/2;
FileName = (bincounts);
FileName=histogram(R_sqr);
legend
hold on
end

Sign in to comment.

More Answers (0)

Tags

Asked:

on 22 Nov 2019

Edited:

on 22 Nov 2019

Community Treasure Hunt

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

Start Hunting!