How to plot in multiple for loops?

1 view (last 30 days)
Pree
Pree on 4 Sep 2015
Answered: Matt Cohen on 8 Sep 2015
I have attached my code here. So I want to plot 2 histograms, one for sleep difference and the other for wake difference but I want to do this for all the subjects and for all 6 weeks. There are 2 for loops, one is for different subjects and the other is for different weeks data of each subject.
I have tried putting hold on in front of the loops but it still only plots the final week's data. How can I make it plot so that it plots data for all the subjects for all 6 weeks on the same figure.
  2 Comments
Adam
Adam on 4 Sep 2015
You mean you want 6 histograms in 1 plot?
Have you checked that there is only one graphics object (histogram) on your axes using the plot browser (or the 'Children' property of your axes)? It may just be that they are all plotting on top of each other so you can only see the last one.
I don't really know what I would expect from 6 histograms in 1 plot. It doesn't seem to make sense to me conceptually unless they are stacked or cover non-overlapping data ranges.
Pree
Pree on 4 Sep 2015
Edited: Pree on 4 Sep 2015
I want 2 histograms in 1 plot (one for sleep difference and the other for wake difference). I want to repeat this for other subjects and for all 6 different weeks on the same plot. At the moment, it reads all the weeks data but only plots the final week's data.
I have plotted it separately for different weeks for different subjects but I want to see the overall picture. Like I want to know how far the data spreads. I don't think it is overlapping because I have put drawnow syntax which updates figures as it runs so I can see that it is not overlapping.

Sign in to comment.

Answers (1)

Matt Cohen
Matt Cohen on 8 Sep 2015
Hi Pree,
I understand that you are interested in plotting two histograms, one for sleep difference and one for wake difference, with each histogram containing the data for several subjects across six different weeks on the same plot.
There are several possible ways of accomplishing this, but the method depends on how you intend to convey this information. There are multiple variables that you are trying to display information about in a single plot, but you are constrained by what the histogram accomplishes. Since you are using a histogram, you do not have full control over the x-axis, since this axis will show the overall bin information. The y-axis shows the count information. You are left with little room to display the subject and week information.
One way to plot all of this information is to use a different bar in the histogram for each week. This would only work for a single subject at a time. You could iterate over the files and store the data for each subject in a single matrix, one week per column. Creating a histogram using the "hist" function with this matrix would give groups of bars, with each bar in a group corresponding to a certain week. With this approach, you would have to make a separate subplot figure for each user. The code snippet below shows an example of this process.
% Grouped Histogram Approach
% (each bar in the group corresponds to a certain week)
% Assume data files have been read in, and this matrix
% corresponds to data for one user across all six weeks.
X = rand(100,6);
figure;
hist(X);
title('Grouped Histogram - Single User Across Six Weeks')
Another way to display this information would be to use groupings of bars for weeks (each group corresponds to a single week) and stacked bars for subjects (each bar is broken into partitions for each user). With this approach, you could include all of the information in a single subplot. This is not easily accomplished using any single existing MATLAB function. To do this, you could try using some different submissions in the MATLAB File Exchange. For example, below is the link to a submission for plotting groups of stacked bars:
This might accomplish something similar to what you are hoping to produce.
I hope this helps.
Matt Cohen

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!