Problem with subplotting the results

1 view (last 30 days)
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

Accepted Answer

Go Sugar
Go Sugar on 4 Oct 2022
Now my code works as I want. Thank you for all the help. =) Below is the correct code.
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
subplot(2,1,1);
hold on
for k=1:length(dataA)
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
subplot(2,1,2);
hold on
for k=1:length(dataB)
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

More Answers (1)

Image Analyst
Image Analyst on 2 Oct 2022
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 Comments
Go Sugar
Go Sugar on 3 Oct 2022
Edited: Go Sugar on 4 Oct 2022
I have attached the plots in the pdf files. While the first and second pages include the output of my code (barplots), the third page consists of what I want to do. What I want: Figure 1 and 2 at the same figure, figure 3 and 4 at the same figure, figure 5 and 6 at the same figure, figure 7 and 8 at the same figure and figure 9 and 10 at the same figure. This is shown on page 3 of pdf file. One of them is shown for figures 1 and 2. I also want the other figures similarly.
I hope all of these clarify my question. Thank you. =)
By the way, I tried writing subplot(2,1,1) and sublot(2,1,2) instead of figure in order. I finally achieved 2 plots but it just overwrites it.
Go Sugar
Go Sugar on 4 Oct 2022
I have solved the problem thank you for all the help. =)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!