Matlab收集数据绘制箱型图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
%读取实验数据并绘制箱型图
%{
##########################################################
使用手册:

数据文档:(Data Text) 软件生成的文本文档,例如:qjy_q1-1_20210827181142。
数据文件夹:(Date File) 使用软件时创建的文件夹,例如:20210827;约定:数据文件夹的名称按“年月日”连续数字来命名,不按照约定命名可能导致读取异常。

设置文件夹:
首先设置matlab的“当前文件夹”为包含“数据文件夹”的文件夹
根据提示输入“数据文件夹”名称

##########################################################
%}
prompt = '请输入数据文件夹名称:';
str = input(prompt);
DataFileName = sprintf('%d',str);%读入“数字”并转化为“文件夹名”

stop_tag = length({DataFileInf.name});%先转化成元胞数组,然后统计元胞数量作为截至循环标签

n=1;
while n+2<=stop_tag
DataText(n).Name=DataFileInf(n+2).name;

userpath(DataFileInf(1).folder);%修改当前文件夹为“数据文件夹”

fileID = fopen(DataText(n).Name);
CellMidDatas = textscan(fileID,'%s %*[^\n]');%以cell形式提取文件的第一列
fclose(fileID);

mid = CellMidDatas{1};

C_usfull_data = [mid(3,1),mid(8,1),mid(9,1),mid(10,1)];%提取有用信息
%{
==================
J_sc=mid(3,1);
V_oc=mid(8,1);
FF=mid(9,1);
Eta = mid(10,1);
===================
%}
usfull_data_str = cell2mat(C_usfull_data);%将元胞数组转化为普通数组(单引号字符串的数组形式会自动合并)
C_usfull_data_num = regexp(usfull_data_str,'\d*\.?\d*','match');%提取字符串中的数字并以元胞数组形式储存
usfull_data_num=string(C_usfull_data_num);%将元胞数组转换为普通(双引号字符串)数组
usfull_data = str2double(usfull_data_num);%将字符串转化为数字
Data(n,:) = usfull_data(1,:);
n=n+1;
end
J_sc = Data(:,1);
V_oc = Data(:,2);
FF = Data(:,3);
Eta = Data(:,4);

disp('统计数据完成');




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
%读取实验数据并绘制箱型图
%使用前需在matlab的“当前文件夹”中考入数据文件夹(如20210827)
clear all
clc

str = input('请输入数据文件夹名称:');
DataFileName = sprintf('%d',str);

DataFileInf=dir(DataFileName);
stop_tag = length({DataFileInf.name});%先转化成元胞数组,然后统计元胞数量作为截至循环标签

n=1;
while n+2<=stop_tag
DataText(n).Name = DataFileInf(n+2).name;

userpath(DataFileInf(1).folder);

fileID = fopen(DataText(n).Name);
CellMidDatas = textscan(fileID,'%s %*[^\n]');
fclose(fileID);

mid = CellMidDatas{1};

C_usfull_data = [mid(3,1),mid(8,1),mid(9,1),mid(10,1)];

usfull_data_str = cell2mat(C_usfull_data);
C_usfull_data_num = regexp(usfull_data_str,'\d*\.?\d*','match');%提取正数
%C_usfull_data_num = regexp(usfull_data_str, '-?\d*\.?\d*', 'match');%可以提取负数
usfull_data_num = string(C_usfull_data_num);
usfull_data = str2double(usfull_data_num);
Data(n,:) = usfull_data(1,:);

n=n+1;
end
disp('统计数据完成');
Data
J_sc = Data(:,1);
V_oc = Data(:,2);
FF = Data(:,3);
Eta = Data(:,4);
%================================================================================
for n=19:32
box2_3(n-18,1)=J_sc(n,1);
end
for n=36:53
box2_3(n-35+14,1) = J_sc(n,1);
end
for n=1:9
sqr(n,1) = J_sc(n,1);
end
for n=10:18
box1_2(n-9,1) = J_sc(n,1);
end
for n=33:35
box1_2(n-32+9,1) = J_sc(n,1);
end
for n=54:62
box1_2(n-53+9+3,1) = J_sc(n,1);
end
y1 = repmat("box2_3",length(box2_3),1);
y2 = repmat("box11_2",length(box1_2),1);
y3 = repmat("sqr",length(sqr),1);
X=vertcat(box2_3,box1_2,sqr);
Y=vertcat(y1,y2,y3);
boxplot(X,Y);

%================================================================================
judge=input('绘图or生成表格:','s');
if judge == '绘图'
%查询片的数量:
PIECE=input('片的数量?\n输入示例:8\n');
CELL=input('电池的数量?\n输入示例:[9 9 9 9 9 9 9 9]\n');

%定义分类方式:
IpMid=input('输入分类方式,将作为箱型图的X轴.\n输入示例:2-3石墨盒 1-2石墨盒\n','S');
class_way = string(split(IpMid));
m=length(class_way);

%对应分类方式与片的关系:
for n=1:m
fprintf('第%d个条件对应的片的序号\n输入示例:[1 3 5]',n);
Part(n,:) = {input('\n')};
Length(n,:)=length(Part{n,:});
end

%画图
for m=1:CELL(1)
load_data_plot(1,m) = J_sc(m);
end
for n=2:PIECE
for m=1:CELL(n)
load_data_plot(n,m) = J_sc((n-1)*CELL(n-1)+m);
end
end
for n=1:m
for q=1:Length
data_plot(n,:)={load_data_plot(Part{n,1}(1,q),:)}
end
end




Plot_title=input('输入图像的名称:','s');
X_name='条件';
Y_name=input('输入Y轴的名称:','s');
switch Y_name
case 'J_sc'
a=1;

case 'V_oc'
a=2;
case 'FF'
a=3;
case 'Eta'
a=4;
otherwise
warning('输入错误,请按照要求输入。')
end
title(Plot_title);


elseif judge == "生成表格"

else
disp("输入错误,请输入“绘图”或者“生成表格”。");
end



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
%读取实验数据并绘制箱型图
%使用前需在matlab的“当前文件夹”中考入数据文件夹(如20210827)
clear all
clc

str = input('请输入数据文件夹名称:');
DataFileName = sprintf('%d',str);

DataFileInf=dir(DataFileName);
stop_tag = length({DataFileInf.name});%先转化成元胞数组,然后统计元胞数量作为截至循环标签

n=1;
while n+2<=stop_tag
DataText(n).Name = DataFileInf(n+2).name;

userpath(DataFileInf(1).folder);

fileID = fopen(DataText(n).Name);
CellMidDatas = textscan(fileID,'%s %*[^\n]');
fclose(fileID);

mid = CellMidDatas{1};

C_usfull_data = [mid(3,1),mid(8,1),mid(9,1),mid(10,1)];

usfull_data_str = cell2mat(C_usfull_data);
C_usfull_data_num = regexp(usfull_data_str,'\d*\.?\d*','match');%提取正数
%C_usfull_data_num = regexp(usfull_data_str, '-?\d*\.?\d*', 'match');%可以提取负数
usfull_data_num = string(C_usfull_data_num);
usfull_data = str2double(usfull_data_num);
Data(n,:) = usfull_data(1,:);

n=n+1;
end
disp('统计数据完成');
Data
J_sc = Data(:,1);
V_oc = Data(:,2);
FF = Data(:,3);
Eta = Data(:,4);
%================================================================================
for n=19:32
box2_3(n-18,1)=J_sc(n,1);
end
for n=36:53
box2_3(n-35+14,1) = J_sc(n,1);
end
for n=1:9
sqr(n,1) = J_sc(n,1);
end
for n=10:18
box1_2(n-9,1) = J_sc(n,1);
end
for n=33:35
box1_2(n-32+9,1) = J_sc(n,1);
end
for n=54:62
box1_2(n-53+9+3,1) = J_sc(n,1);
end

y1 = repmat("box2_3",length(box2_3),1);
y2 = repmat("box1_2",length(box1_2),1);
y3 = repmat("sqr",length(sqr),1);
X=vertcat(box2_3,box1_2,sqr);
Y=vertcat(y1,y2,y3);

figure('Name','Jsc');
boxplot(X,Y);
xlabel('condition');
ylabel('Jsc');
title('The boxplot of Jsc');
%==============================================================================
for n=19:32
box2_3(n-18,1)=V_oc(n,1);
end
for n=36:53
box2_3(n-35+14,1) = V_oc(n,1);
end
for n=1:9
sqr(n,1) = V_oc(n,1);
end
for n=10:18
box1_2(n-9,1) = V_oc(n,1);
end
for n=33:35
box1_2(n-32+9,1) = V_oc(n,1);
end
for n=54:62
box1_2(n-53+9+3,1) = V_oc(n,1);
end

y1 = repmat("box2_3",length(box2_3),1);
y2 = repmat("box1_2",length(box1_2),1);
y3 = repmat("sqr",length(sqr),1);
X=vertcat(box2_3,box1_2,sqr);
Y=vertcat(y1,y2,y3);

figure('Name','Voc');
boxplot(X,Y);
xlabel('condition');
ylabel('Voc');
title('The boxplot of Voc');
%==============================================================================
for n=19:32
box2_3(n-18,1)=FF(n,1);
end
for n=36:53
box2_3(n-35+14,1) = FF(n,1);
end
for n=1:9
sqr(n,1) = FF(n,1);
end
for n=10:18
box1_2(n-9,1) = FF(n,1);
end
for n=33:35
box1_2(n-32+9,1) = FF(n,1);
end
for n=54:62
box1_2(n-53+9+3,1) = FF(n,1);
end

y1 = repmat("box2_3",length(box2_3),1);
y2 = repmat("box1_2",length(box1_2),1);
y3 = repmat("sqr",length(sqr),1);
X=vertcat(box2_3,box1_2,sqr);
Y=vertcat(y1,y2,y3);

figure('Name','FF');
boxplot(X,Y);
xlabel('condition');
ylabel('FF');
title('The boxplot of FF');
%==============================================================================
for n=19:32
box2_3(n-18,1)=Eta(n,1);
end
for n=36:53
box2_3(n-35+14,1) = Eta(n,1);
end
for n=1:9
sqr(n,1) = Eta(n,1);
end
for n=10:18
box1_2(n-9,1) = Eta(n,1);
end
for n=33:35
box1_2(n-32+9,1) = Eta(n,1);
end
for n=54:62
box1_2(n-53+9+3,1) = Eta(n,1);
end

y1 = repmat("box2_3",length(box2_3),1);
y2 = repmat("box1_2",length(box1_2),1);
y3 = repmat("sqr",length(sqr),1);
X=vertcat(box2_3,box1_2,sqr);
Y=vertcat(y1,y2,y3);

figure('Name','Eta');
boxplot(X,Y);
xlabel('condition');
ylabel('Eta');
title('The boxplot of Eta');