clear clc pathname = uigetdir; name_list=dir(pathname); for i=3:22 name_list(i).引用了不存在的字段 'num'。 出错 LoadData (line 33)num=zeros(20,1); name_list(i).num(i-2)=1; end %pathname = uigetdir; img_name1=importdata('E:\\train.txt'); img_train_num=size(img_name1,1); num=0; for i=1:img_train_num img_name_char=cell2mat(img_name1(i)); label_1(i).name=extractBefore(cell2mat(img_name1(i)),'_'); imgPath = [pathname,'\',label_1(i).name,'\',cell2mat(img_name1(i))]; temp = imread(imgPath); train_x(:,:,i)=temp; % temp = double(temp(:)); % num=num+1; % imagedata1(:,num)=temp; end for i=1:img_train_num for j=1:20 if strcmp(name_list(j+2).name,label_1(i).name) label_1(i).num=name_list(j+2).num; end end train_y(:,i)=uint8(label_1(i).num); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %pathname = uigetdir; img_name1=importdata('E\\test.txt'); img_test_num=size(img_name1,1); num=0; for i=1:img_test_num img_name_char=cell2mat(img_name1(i)); label_2(i).name=extractBefore(cell2mat(img_name1(i)),'_'); imgPath = [pathname,'\',label_2(i).name,'\',cell2mat(img_name1(i))]; temp = imread(imgPath); test_x(:,:,i)=temp; % temp = double(temp(:)); % num=num+1; % imagedata2(:,num)=temp; end for i=1:img_test_num for j=1:20 if strcmp(name_list(j+2).name,label_2(i).name) label_2(i).num=name_list(j+2).num; end end test_y(:,i)=uint8(label_2(i).num); end save('E\\imgdata_uint8.mat', 'train_x','train_y','test_x','test_y');
时间: 2024-01-20 08:04:08 浏览: 90
clc.rar_clc_matlab 中值滤波_中值_中值滤波 S函数_中值滤波 matlab
这段代码的作用是从指定文件夹中读取图像数据和标签,并将它们保存到.mat文件中。但是,代码中有一些错误:
1. `name_list(i).num(i-2)=1;` 这行代码是错误的,因为`name_list`结构体中没有`num`这个字段。我猜测你想要的是给`label_1`结构体中的`num`字段赋值,因此应该改为`label_1(i).num=1;`。
2. `img_name1=importdata('E:\\train.txt');` 和 `img_name1=importdata('E\\test.txt');` 这两行代码中的路径分隔符不一致,应该都使用反斜杠`\`。
3. `save('E\\imgdata_uint8.mat', 'train_x','train_y','test_x','test_y');` 这行代码中的路径分隔符也应该改为反斜杠`\`。另外,如果你想要在`E:`盘保存文件,应该使用双反斜杠`\\`,因为单反斜杠`\`是转义符。
修改后的代码如下:
```
clear
clc
pathname = uigetdir;
name_list = dir(pathname);
for i = 3:22
label_1(i-2).num = 0;
end
img_name1 = importdata('E:\train.txt');
img_train_num = size(img_name1, 1);
for i = 1:img_train_num
img_name_char = cell2mat(img_name1(i));
label_1(i).name = extractBefore(cell2mat(img_name1(i)), '_');
imgPath = [pathname, '\', label_1(i).name, '\', cell2mat(img_name1(i))];
temp = imread(imgPath);
train_x(:,:,i) = temp;
end
for i = 1:img_train_num
for j = 1:20
if strcmp(name_list(j+2).name, label_1(i).name)
label_1(i).num = name_list(j+2).num;
end
end
train_y(:,i) = uint8(label_1(i).num);
end
for i = 1:img_train_num
label_2(i).num = 0;
end
img_name1 = importdata('E:\test.txt');
img_test_num = size(img_name1, 1);
for i = 1:img_test_num
img_name_char = cell2mat(img_name1(i));
label_2(i).name = extractBefore(cell2mat(img_name1(i)), '_');
imgPath = [pathname, '\', label_2(i).name, '\', cell2mat(img_name1(i))];
temp = imread(imgPath);
test_x(:,:,i) = temp;
end
for i = 1:img_test_num
for j = 1:20
if strcmp(name_list(j+2).name, label_2(i).name)
label_2(i).num = name_list(j+2).num;
end
end
test_y(:,i) = uint8(label_2(i).num);
end
save('E:\imgdata_uint8.mat', 'train_x', 'train_y', 'test_x', 'test_y');
```
阅读全文