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!