Search code examples
matlabplotsignal-processingmatlab-figure

Plot a bar chart as subplot in a for loop from a table so that it updates at each iteration w.r.t row elements


I have several subplots in a for loop of subjects. I want to plot the subject wise performance from a 20x2 table. How do I make a separate subplot- a barchart on the same figure in a way that at first iteration it updates and plot the elements of 1st row as Space and Time respectively, then on 2nd iteration that of second row and so on, from the performance table? An excerpt from my code is given below.

Thanks in advance for your help!

Best, A

for s=1:size(SubjectFolder,2)
                    % ..... some code
figure(1)
 subplot(423)
                        f_psd = Grand_AvgTF_Space.freq;
                        [trash,f_psd_plot] = min(abs(f_psd-which_f)); 
                        PSD = mean(abs(squeeze(Grand_AvgTF_Space.powspctrm(s,:,:,:))),3); % [ch x freq] PSD of space or time condition          
                        semilogy(f_psd,PSD,'color','b')
                        hold on, 
                        semilogy(f_psd, mean(PSD),'linewidth',3,'color','r')
                        hold on
                        line([which_f which_f],[min(PSD(:)) max(PSD(:))],'color','g','linewidth',3)
                        xlabel('Frequency (Hz)')
                        ylabel('Power (dB/Hz)')
                        title( 'Space' )
                        ylim([min(PSD(:)) max(PSD(:))])
                        title( 'Space' )
    subplot(424), % topoplot space
                    data_megin.avg = repmat(PSD(:,f_psd_plot),1,length(data_megin.time));
                    cfg.parameter        = 'avg' %
                    ft_topoplotER(cfg, data_megin );
                    title(['Topolot for Space Freq = ' num2str(f_psd(f_psd_plot)) ' Hz'])
                    
                    %..... some code
            end 
        
            % 20x2 Table looks like this without variable names. Suppose I wanna give 1st col name as Space and 2nd col as Time in the required subplot, where each row represents subject1 to subject20.
            96   100
            85   89
            .     .
            .     .
            80    87

enter image description here

enter image description here


Solution

  • Something on the lines of this might work, Unfortunately, without the data I resorted to using placeholder plots. The size and colours of the bar graph can most likely be changed as well. Here I assume the topographical plots can take a subplot slot without any issues. I'm not familiar enough with the plotting characteristics/behaviour of topographical plots to judge if this will work as expected.

    For a smaller bar graph change this line from:

    subplot(4,3,[3 6 9 12]); Bar_Graph = bar(Bar_Data);
    

    to:

    subplot(4,3,[6 9]); Bar_Graph = bar(Bar_Data);
    

    Subplots Using Multiple Slots

    Table = round(100*rand(20,2));
    Placeholder = zeros(500,1000);
    
    for Figure_Index = 1: 20
    
    figure(Figure_Index);
    subplot(4,3,1); imshow(Placeholder); title("Plot 1");
    subplot(4,3,2); imshow(Placeholder); title("Topoplot 1");
    subplot(4,3,4); imshow(Placeholder); title("Plot 2");
    subplot(4,3,5); imshow(Placeholder); title("Topoplot 2");
    subplot(4,3,7); imshow(Placeholder); title("Plot 3");
    subplot(4,3,8); imshow(Placeholder); title("Topoplot 3");
    subplot(4,3,10); imshow(Placeholder); title("Plot 4");
    subplot(4,3,11); imshow(Placeholder); title("Topoplot 4");
    
    Bar_Data = Table(Figure_Index,:);
    subplot(4,3,[3 6 9 12]); Bar_Graph = bar(Bar_Data);
    text(1:length(Bar_Data),Bar_Data,num2str(Bar_Data'),'vert','bottom','horiz','center'); 
    Bar_Graph.BaseLine.Visible = 'off';
    axis off
    
    end
    

    Ran using MATLAB R2019b