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

 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 2 de Oct. de 2022
Thank you for the fast reply. =)
j=bar(k,dataB(k)) produces 1 bar plot up to ength(dataB) for each p value according to the conditions I have written. I want to produce bar plots of A and B for each p (not for each k). Then I tried something similar and put p instead of k according to your code, it still does not work. I still do not understand why it does not work. =)
Image Analyst
Image Analyst el 2 de Oct. de 2022
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
Go Sugar el 2 de Oct. de 2022
I have just attached the data. As you can see, there are A and B values (for 29 trials) for 5 subjects. My above code barplots each variable for each participant. As a result I have 2 separate figures (A and B for each subject). I want to combine plots of A and B (2 figures) in one figure for each participant. I tried subplotting, it does not give correct values.
Or another idea may be saving these figures and combining them later.
Thank you su much. =) I appreciate your help.
Image Analyst
Image Analyst el 2 de Oct. de 2022
There is no chart in there. I think you missed my last comment. Please show us what you want.
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.

Categorías

Más información sobre Axes Appearance en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Oct. de 2022

Comentada:

el 4 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by