Problem with subplotting the results
이전 댓글 표시
Hello =),
I have 5 subjects' data. Below code produces bar plots for each subject (2 separate figures (A and B) for each subject: a total of 10 plots (10 figures)). My code works perfectly but when I add subplot in my code, it does not produce the correct results. I want to plot the measures of each subject (A and B) separately at the same figure (subject 1: both plots on the same figure 1, subject 2: both plots on the same figure 2....., subject 5: both plots on the same figure 5) so that there are a total of 5 distinct figures.
Since I am having problems with the subplot, I have used the same for loop for measures A and B. It would be better if I use 1 for loop for measures A and B with subplots.
Thank you so much. =)
filename= strcat('a.xlsx');
data=xlsread(filename);
for p=1:5
dataA=data(p,2:30)
dataB=data(p,31:59)
% for measure A
figure
hold on
for k=1:length(dataA)
% subplot(2,1,1); %When I enter this, it does not work.
j=bar(k,dataA(k));
if dataA(k)==1 & dataB(k)==1
set(j,'FaceColor','b');
elseif dataA(k)==1 & dataB(k)==0
set(j,'FaceColor','r');
elseif dataA(k)==2 & dataB(k)==1
set(j,'FaceColor','k');
else
set(j,'FaceColor','y');
end
end
% for measure B
figure
hold on
for k=1:length(dataB)
% subplot(2,1,1); %When I enter this, it does not work.
j=bar(k,dataB(k));
if dataA(k)==1 & dataB(k)==1
set(j,'FaceColor','b)';
elseif dataA(k)==1 & dataB(k)==0
set(j,'FaceColor','r');
elseif dataA(k)==2 & dataB(k)==1
set(j,'FaceColor','k');
else
set(j,'FaceColor','y');
end
end
end
채택된 답변
추가 답변 (1개)
Image Analyst
2022년 10월 2일
You're putting all the plots in the same slot when you say "subplot(2,1,1)" : into the first row, first column of a 2 row, 1 column layout. If you want length(dataB) subplots on the same figure window you'll have to change the third argument of subplot to k, and have enough slots to fit them all:
numPlots = length(dataB)
plotRows = ceil(sqrt(numPlots))
for k=1:length(dataB)
subplot(plotRows, plotRows, k); % When I enter this, it works.
j=bar(k,dataB(k));
댓글 수: 6
Go Sugar
2022년 10월 2일
Image Analyst
2022년 10월 2일
Attach your data and mock up something that shows what you want - use Photoshop if you have to. Or look here:
Perhaps just pass in a whole vector or whole matrix if you want multiple bars plotted at once.
Go Sugar
2022년 10월 2일
Image Analyst
2022년 10월 2일
There is no chart in there. I think you missed my last comment. Please show us what you want.
Go Sugar
2022년 10월 4일
카테고리
도움말 센터 및 File Exchange에서 Axes Appearance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!