matlab - Looping through columns to make subplots -
i have spectral absorbance matrix in xls
format has column 1 wavelength, , columns 2:3 associated absorbance spectra particles depth1 in water column. 2:3 duplicates have plotted together. next have columns 4:5 once again duplicate absorbance spectra particles depth2 in water column. data depth3, depth4 etc. matrix 1001 rows 13 columns.
i have 3 subplots (depth1, depth2 , depth3) in 1 figure each subplot holding 2 duplicate spectra of each depth.
i tried follow excellent responses this question gives me 1 line per subplot want plot 2 lines (duplicate spectra). did following , works can 3 subplots:
[num,txt,raw]=xlsread('anacondas2010-st1.xls'); legendcell=cellstr(txt); figure subplot(3,1,1) plot(num(:,1), num(:,2:3),'r');grid on; box on; xlabel('wavelength'), ylabel('absorbance'),legend(legendcell(2:3)),legend boxoff subplot(3,1,2) plot(num(:,1), num(:,4:5),'b');grid on; box on; xlabel('wavelength'), ylabel('absorbance'),legend(legendcell(4:5)), legend boxoff subplot(3,1,3) plot(num(:,1), num(:,6:7),'g');grid on; box on; xlabel('wavelength'), ylabel('absorbance'),legend(legendcell(6:7)), legend boxoff title('station 1','fontweight','bold','fontsize',16);
but can see gives me 1 figure 3 subplots , rest od depths (d4, d5, d6) remain unplotted havent been able specifiy them,
because script long , cumbersome have liked run through loop couldn't figure out how in spite of battling code provided in 2nd answer kind of understood unlike first one.
updated answer inserted version v2 of code
version v2 of code allows displaying unlimited pairs of data, simpler v1.
% generation of example data num=1:33; number_of_data_colums=14; num=[num' rand(length(num),number_of_data_colums)]; % generation of legend string i=1:number_of_data_colums legendcell{i}=['absor. ' num2str(i)]; end % size of data plotted (columns 2 ...) [r,c]=size(num); n_data=floor((c-1)/2); % define number of data plotted in each subplt data_x_plot=2; % consistency check: if number of column data not generate % error message , exit if(n_data*2 ~= (c-1)) error('number of data columns not even') else % define number of subplot of each figure n_sub_plot=3; % subplot , figure counters s_plot_cnt=1; fig_cnt=1; % create first figure figure % external loop on figures i=2:2:n_data*2 % if 3 subplot have been added figure, open new figure if(s_plot_cnt == 4) % title assigne first subplot of each figure title(ax(fig_cnt,1),['station ' num2str(fig_cnt)],'fontweight','bold','fontsize',16); s_plot_cnt=1; fig_cnt=fig_cnt+1; figure end ax(fig_cnt,s_plot_cnt)=subplot(n_sub_plot,1,s_plot_cnt); % indices of columns plotted computed automatically plot(num(:,1), num(:,i:i+1)); grid on; box on; xlabel('wavelength') ylabel('absorbance') % add legend legend(legendcell(i-1:i),-1) % increment subplot's counter s_plot_cnt=s_plot_cnt+1; % legend boxoff end end % add last title title(ax(fig_cnt,1),['station ' num2str(fig_cnt)],'fontweight','bold','fontsize',16);
previous answer and version v1 of code
i'm not sure i've understood question, nevertheless, if have 6 pairs of data , want 3 subplot, need 2 figures.
i've modified original script in order automatically determine number of figure need, generate subplots , plot 2 set of data in each of them.
updated code - legends
% generation of example data num=1:33; num=[num' rand(length(num),12)]; % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % updated code stars here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % generation of legend string legendcell{1}='wavel'; i=2:13 legendcell{i}=['absor. ' num2str(i)]; end % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % updated code ends here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % size of data plotted [r,c]=size(num); % define number of data plotted in each subplt data_x_plot=2; % define number of subplot of each figure n_sub_plot=3; % evaluate number of figures created n_fig=(c-1)/(data_x_plot*n_sub_plot); % define index of data columns idx=[2:2:c-1]; idx=reshape(idx,n_sub_plot,data_x_plot)'; % external loop on figures i=1:n_fig figure % internal loop on subplots j=1:n_sub_plot % subplot indices computed automatically ax(i,j)=subplot(n_sub_plot,1,j); % indices of columns plotted computed automatically plot(num(:,1), num(:,idx(i,j):idx(i,j)+1)); grid on; box on; xlabel('wavelength') ylabel('absorbance') % add legend legend(legendcell(idx(i,j):idx(i,j)+1),-1) % legend boxoff end % title assigne first subplot of each figure title(ax(i,1),['station ' num2str(i)],'fontweight','bold','fontsize',16); end
given set of 12 columns of data, output:
updated graphs, legends
Comments
Post a Comment