只有平均光谱是对的。其他待修改
clc
clear all
% 批量读取一个文件夹txt数据
cell_type = 'pc';
namelist = dir('D:\zhuomian\shuju\pc\*.txt');
l = length(namelist);
% 初始化一个单元数组以存储数据
Data = cell(1, l);
for i = 1:l
file_name = strcat(['D:\zhuomian\shuju\pc\', namelist(i).name]);
Data{i} = load(file_name);
end
% 根据文件名模式将数据分为三个类别
spectra_type1 = [];
spectra_type2 = [];
spectra_type3 = [];
% % 提取波数点和幅值列
wavenumbers = Data{1}(:, 1); % 使用第一个文件的波数点作为模板
values_type1 = [];
values_type2 = [];
values_type3 = [];
for i = 1:l
if contains(namelist(i).name, 'PANC-NC-1')
spectra_type1 = [spectra_type1, Data{i}(:, 1)]; % 更新波数点
values_type1 = [values_type1, Data{i}(:, 2:end)]; % 更新幅值列
elseif contains(namelist(i).name, 'PANC-NC-2')
spectra_type2 = [spectra_type2, Data{i}(:, 1)]; % 更新波数点
values_type2 = [values_type2, Data{i}(:, 2:end)]; % 更新幅值列
elseif contains(namelist(i).name, 'PANC1')
spectra_type3 = [spectra_type3, Data{i}(:, 1)]; % 更新波数点
values_type3 = [values_type3, Data{i}(:, 2:end)]; % 更新幅值列
end
end
% 计算每一行幅值列的均值
mean_spectrum_type1 = mean(values_type1, 2);
mean_spectrum_type2 = mean(values_type2, 2);
mean_spectrum_type3 = mean(values_type3, 2);
% 将均值和波数点存储为新的变量
avg_spectrum_type1 = [spectra_type1(:, 1), mean_spectrum_type1];
avg_spectrum_type2 = [spectra_type2(:, 1), mean_spectrum_type2];
avg_spectrum_type3 = [spectra_type3(:, 1), mean_spectrum_type3];
% 绘制图表
figure;
% 子图1
subplot(3, 1, 1);
plot(avg_spectrum_type1(:, 1), avg_spectrum_type1(:, 2), 'r', 'LineWidth', 2);
title('Average Spectrum for Type 1');
xlabel('Wavenumbers');
ylabel('Mean Spectrum');
grid on;
% 子图2
subplot(3, 1, 2);
plot(avg_spectrum_type2(:, 1), avg_spectrum_type2(:, 2), 'g', 'LineWidth', 2);
title('Average Spectrum for Type 2');
xlabel('Wavenumbers');
ylabel('Mean Spectrum');
grid on;
% 子图3
subplot(3, 1, 3);
plot(avg_spectrum_type3(:, 1), avg_spectrum_type3(:, 2), 'b', 'LineWidth', 2);
title('Average Spectrum for Type 3');
xlabel('Wavenumbers');
ylabel('Mean Spectrum');
grid on;
% 在同一图中绘制三个平均光谱
figure;
plot(avg_spectrum_type1(:, 1), avg_spectrum_type1(:, 2), 'r', 'LineWidth', 2, 'DisplayName', 'Type 1');
hold on;
plot(avg_spectrum_type2(:, 1), avg_spectrum_type2(:, 2), 'g', 'LineWidth', 2, 'DisplayName', 'Type 2');
plot(avg_spectrum_type3(:, 1), avg_spectrum_type3(:, 2), 'b', 'LineWidth', 2, 'DisplayName', 'Type 3');
title('Average Spectra for Types 1, 2, and 3');
xlabel('Wavenumbers');
ylabel('Mean Spectrum');
legend('show');
grid on;
hold off;
% 获取每组数据的平均值
avg_data_type1 = avg_spectrum_type1;
avg_data_type2 = avg_spectrum_type2;
avg_data_type3 = avg_spectrum_type3;
% 初始化存储显著性检验结果的变量
significant_results = cell(0, 7); % 存储行号、p 值和原始数据
% 逐行进行两两比较
for row = 1:size(avg_data_type1, 1)
% 从每组数据中取出对应的行
data1 = avg_data_type1(row, :);
data2 = avg_data_type2(row, :);
data3 = avg_data_type3(row, :);
% 执行 t 检验
[~, p12] = ttest2(data1, data2, 'Alpha', 0.05);
[~, p13] = ttest2(data1, data3, 'Alpha', 0.05);
[~, p23] = ttest2(data2, data3, 'Alpha', 0.05);
% 判断是否显著,如果是就记录下来
if p12 > 0.05 || p13 > 0.05 || p23 > 0.05
% 存储行号、p 值和原始数据
significant_results = [significant_results; {row, p12, p13, p23, data1, data2, data3}];
end
end
% 输出差异显著的行及其 t 检验值和原始数据
disp('Significant Rows:');
disp('Row | P-value (1 vs 2) | P-value (1 vs 3) | P-value (2 vs 3) | Data1 | Data2 | Data3');
disp(significant_results);
% 提取显著性检验结果中的p值
p_values = cell2mat(significant_results(:, 2:4));
% 定义颜色
colors = {'r', 'g', 'b'};
% 绘制折线图来查看p值的差异
figure;
hold on;
for i = 1:3
plot(p_values(i, :), '-o', 'Color', colors{i}, 'LineWidth', 2, 'MarkerSize', 8);
end
hold off;
legend({'1 vs 2', '1 vs 3', '2 vs 3'}, 'Location', 'best');
title('Variation of P-values between Groups');
xlabel('Comparison');
ylabel('P-values');