how to use the 'histfit' function for cell arrays?

Hello, probably this is an easy question, however I was wondering how to use the ´histfit´ function for a cell array in order to plot the data by cells (in one general plot) delimited in the X axis by the cell number? Data attached, the code showed here just plot the entire cell array , without making a difference between cells. , Thank you.
histfit(S7{1,end});

9 Comments

It's not clear to me what you are trying to do. S7 is a 1x12 cell array. Each cell is a very small sample -- 2, 3, 4, or 5 numbers. Your example line of code produces a histogram for the last cell which contains 5 numbers only. Please try to clarify your goal.
@Scott MacKenzie The idea is to produce a complete histogram of the entire S7 cell array. S7 has 12 cells. The X axis should contain the number of cells, and above each number (X axis) the histogram of each cell.
So, the x-axis goes from 1 to 12. At x = 1 is a histogram for the data in cell #1. At x = 2 is a histogram for the data in cell #2. And so on. Is that the idea? Sounds odd to me, given the paucity of data in each cell, not to mention the clutter of 12 histograms side-by-side. But, that's what I'm getting from your description.
@Scott MacKenzie that's the idea, however, if is possible to plot the whole histogram of the S7 array, it also will be fine (I guess is easier and faster the last option).
histfit(S7{1:end});
this way doesn't work like that.
By "plot the whole histogram of the S7 array", do you mean a single histogram with all the data in S7 lumped together?
@Scott MacKenzie yes, how could you do it?, thanks.
Here's one way to do this...
load S7;
y = [];
for i=1:length(S7)
y = [y S7{i}'];
end
histfit(y,10); % 10 bins
There might be a one-line approach to putting the S7 data into a vector -- not sure.
@Scott MacKenzie is there another way to plot a whole histogram of S7 without using a loop?

Sign in to comment.

Answers (1)

Hello,
I understand that you're looking to create a histogram for all the values within your cell array ‘S7’ combined, and you're aiming for a solution that avoids loops, ideally condensing it into a single line of code.
This can be achieved by concatenating all cell contents vertically using ‘cat(1, S7{:})’, which stacks the data from each cell into a single column vector. Then, we can apply histfit directly to this vector. Here's the streamlined code:
load('S7.mat')
histfit(cat(1,S7{:}),10)
For more details on the cat function and its usage, you can refer to the following link: https://www.mathworks.com/help/matlab/ref/double.cat.html
Hope this helps

Products

Release

R2020b

Asked:

on 3 Jun 2021

Edited:

on 22 Feb 2024

Community Treasure Hunt

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

Start Hunting!