我有xls
格式的光谱吸收矩阵,其中第1列为波长,第2列为第3:3列,作为水柱深度1的粒子的相关吸收光谱。 2:3是重复的,因此它们必须一起绘制。接下来,我将列4:5再次复制来自水柱深度2的粒子的吸收光谱。然后是depth3,depth4等的数据。矩阵是1001行乘13列。
我希望在一个图中有3个子图(depth1,depth2和depth3),每个子图包含每个深度的2个重复光谱。
我试图遵循对this question的出色反应,但这给了我每个子图一行,但我想绘制两行(重复光谱)。所以我做了以下工作但它有效,但我只能获得3个子图:
[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);
但正如你所看到的那样,这只给我一个带有3个子图的数字,其余的深度(d4,d5,d6)保持未开槽,因为我还没有能够指定它们,
因为我的脚本很长而且很麻烦,所以我希望通过一个循环来运行它但是我无法弄清楚如何使用第二个答案中提供的代码来解决这个问题,我有点理解不像第一个。
答案 0 :(得分:1)
更新回答 插入代码版本V2
代码版本V2允许显示无限数据对,也比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
for i=1:number_of_data_colums
legendCell{i}=['Absor. ' num2str(i)];
end
% Get the size of data to be plotted (columns from 2 to ...)
[r,c]=size(num);
n_data=floor((c-1)/2);
% Define the number of data to be plotted in each subplt
data_x_plot=2;
% Consistency check: if the number of column data is not even generate an
% error message and exit
if(n_data*2 ~= (c-1))
error('Number of data columns is not even')
else
% Define the number of subplot of each figure
n_sub_plot=3;
% Subplot and figure counters
s_plot_cnt=1;
fig_cnt=1;
% Create the first figure
figure
% External loop on figures
for i=2:2:n_data*2
% If three subplot have been added to a figure, open a new figure
if(s_plot_cnt == 4)
% The Title is assigne to the 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);
% The indices of columns to be plotted are 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 the last title
title(ax(fig_cnt,1),['STATION ' num2str(fig_cnt)],'fontweight','bold','fontsize',16);
以前的答案和 代码版本V1
我不确定我是否理解了您的问题,但是,如果您有6对数据且需要3个子图,则需要2个数字。
我修改了原始脚本,以便自动确定所需的图形数量,生成子图并在每个图中绘制2组数据。
更新了代码 - 现在使用了图例
% Generation of example data
num=1:33;
num=[num' rand(length(num),12)];
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UPDATED CODE STARS HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Generation of legend string
legendCell{1}='Wavel';
for i=2:13
legendCell{i}=['Absor. ' num2str(i)];
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UPDATED CODE ENDS HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Get the size of data to be plotted
[r,c]=size(num);
% Define the number of data to be plotted in each subplt
data_x_plot=2;
% Define the number of subplot of each figure
n_sub_plot=3;
% Evaluate the number of figures to be created
n_fig=(c-1)/(data_x_plot*n_sub_plot);
% Define the index of data columns
idx=[2:2:c-1];
idx=reshape(idx,n_sub_plot,data_x_plot)';
% External loop on figures
for i=1:n_fig
figure
% Internal loop on subplots
for j=1:n_sub_plot
% The subplot indices are computed automatically
ax(i,j)=subplot(n_sub_plot,1,j);
% The indices of columns to be plotted are 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
% The Title is assigne to the first subplot of each figure
title(ax(i,1),['STATION ' num2str(i)],'fontweight','bold','fontsize',16);
end
给定一组12列数据,这是输出:
使用图例
更新了图表
答案 1 :(得分:0)
在添加新图时,也许您应该使用Hold来保留当前的图。 所以你的代码应该是这样的:
[num,txt,raw]=xlsread('ANACONDAS2010-ST1.xls');
legendCell=cellstr(txt);
figure
subplot(3,1,1)
plot(num(:,1), num(:,2),'r');grid on;
hold on;
plot(num(:,1), num(:,3),'m');
box on; xlabel('Wavelength'), ylabel('Absorbance'),legend(legendCell(2:3)),legend boxoff
subplot(3,1,2)
plot(num(:,1), num(:,4),'b');
hold on;
plot(num(:,1), num(:,5),'c');
grid on; box on; xlabel('Wavelength'), ylabel('Absorbance'),legend(legendCell(4:5)), legend boxoff
subplot(3,1,3)
plot(num(:,1), num(:,6),'g');
hold on;
plot(num(:,1), num(:,7),'k');
grid on; box on; xlabel('Wavelength'), ylabel('Absorbance'),legend(legendCell(6:7)), legend boxoff
title('STATION 1','fontweight','bold','fontsize',16);