Help making a histogram-like bar graph

조회 수: 4 (최근 30일)
Lindsay
Lindsay 2014년 11월 26일
Hi, I'm trying to compile data from 3 different variables (ST, SM, and SV) onto 1 stacked bar graph. I want my x-axis to be my variables of interest, and the y-axis to be like a histogram. I want to use the bar function so I have some more flexibility. My adviser helped me put together some code but I think I'm still unclear on how the bar graph function works. Here is an example of my code:
%%cycle through all subjects, importing data from Excel for iSubj = 1:length(SUBJECTS)
%%import relevant data from Excel
qthres = xlsread(xlsfile, iSubj,'J3:J16');
scalae = xlsread(xlsfile, iSubj, 'C3:C16');
%finds scalae
[STi, STj, ST] = (find(scalae == 1 & qthres > 0));
[SMi, SMj,SM] = (find(scalae == 2 & qthres > 0));
[SVi, SVj,SV] = (find(scalae == 3 & qthres > 0));
thresST = qthres(STi);
thresSM = qthres(SMi);
thresSV = qthres(SVi);
%store scalae info for each subject
allST = cat(1, allST, thresST);
allSM = cat(1, allSM, thresSM);
allSV = cat(1, allSV, thresSV);
end
%%plot histogram%%
figure(1);
BIN = 3;
ST = hist(allST,[min(allST):BIN:max(allST)]);
SM = hist(allSM,[min(allST):BIN:max(allST)]);
SV = hist(allSV,[min(allST):BIN:max(allST)]);
barplotST = [ST'];
hbarST = bar([min(allST):BIN:max(allST)],barplotST, 'stacked');
set(hbarST, 'FaceColor', 'k', 'EdgeColor', 'k');
hold all;
barplotSM = [SM'];
hbarSM = bar([min(allST):BIN:max(allST)],barplotSM, 'stacked');
set(hbarSM, 'FaceColor', 'g', 'EdgeColor', 'g');
barplotSV= [SV'];
hbarSV = bar([min(allST):BIN:max(allST)],barplotSV, 'stacked');
set(hbarSV, 'FaceColor', 'b', 'EdgeColor', 'b');
I separated everything out so I could make each variable its own color. Perhaps there is a better way to do this and to get the y-axis as a "frequency" or "count." The reason I ask is when I do this with another variable of interest, certain vectors end up empty. I think it has to do with how I"m plotting the y-axis.
Any help to simplify this? Thanks!

답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by