Problem with subplotting the results

2 visualizaciones (últimos 30 días)
Go Sugar
Go Sugar el 2 de Oct. de 2022
Comentada: Go Sugar el 4 de Oct. de 2022
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

Respuesta aceptada

Go Sugar
Go Sugar el 4 de Oct. de 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

Más respuestas (1)

Image Analyst
Image Analyst el 2 de Oct. de 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 comentarios
Go Sugar
Go Sugar el 3 de Oct. de 2022
Editada: Go Sugar el 4 de Oct. de 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 el 4 de Oct. de 2022
I have solved the problem thank you for all the help. =)

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by