MATLAB 读取图片时,一直显示“Reference to non-existent filed "


function [text,pic1,pic2] = PictureRead(f1,f2)
animal = num2str(f1);
holding = num2str(f2);
path1 = 'E:/動物/';
path2 = 'E:/持有物/';
file_list1 = dir(fullfile(path1, '*.PNG'));
file_list2 = dir(fullfile(path2, '*.PNG'));
%由于数字组合有很多种,可自行加上具体组合情况,根据传入的参数即可
switch animal
    case '1'
        text = '棕熊';
        pic1 = imread(strcat(path1,file_list1.bear));
    case '2'
        text = '小猫';
        pic1 = imread(strcat(path1,file_list1.cat));
    case '3'
        text = '小鸡';
        pic1 = imread(strcat(path1,file_list1.chicken));
    case '4'
        text = '小鹿';
        pic1 = imread(strcat(path1,file_list1.corpus));
    case '5'
        text = '小牛';
        pic1 = imread(strcat(path1,file_list1.cow));
    case '6'
        text = '小狗';
        pic1 = imread(strcat(path1,file_list1.dog));
    case '7'
        text = '神龙';
        pic1 = imread(strcat(path1,file_list1.dragon));
    case '8'
        text = '大象';
        pic1 = imread(strcat(path1,file_list1.elephant));
    case '9'
        text = '狐狸';
        pic1 = imread(strcat(path1,file_list1.fox));
    case '10'
        text = '仓鼠';
        pic1 = imread(strcat(path1,file_list1.hamster));
    case '11'
        text = '河马';
        pic1 = imread(strcat(path1,file_list1.hippo));
    case '12'
        text = '小马';
        pic1 = imread(strcat(path1,file_list1.hourse));
    case '13'
        text = '豹子';
        pic1 = imread(strcat(path1,file_list1.leopard));
    case '14'
        text = '狮子';
        pic1 = imread(strcat(path1,file_list1.lion));
    case '15'
        text = '猴子';
        pic1 = imread(strcat(path1,file_list1.monkey));
    case '16'
        text = '老鼠';
        pic1 = imread(strcat(path1,file_list1.mouse));
    case '17'
        text = '熊猫';
        pic1 = imread(strcat(path1,file_list1.panda));
    case '18'
        text = '企鹅';
        pic1 = imread(strcat(path1,file_list1.penguin));
    case '19'
        text = '小猪';
        pic1 = imread(strcat(path1,file_list1.pig));
    case '20'
         text = '兔子';
        pic1 = imread(strcat(path1,file_list1.rabbit));
    case '21'
        text = '浣熊';
        pic1 = imread(strcat(path1,file_list1.raccoon));
    case '22'
        text = '绵羊';
        pic1 = imread(strcat(path1,file_list1.sheep));
    case '23'
        text = '青蛇';
        pic1 = imread(strcat(path1,file_list1.snake));
    case '24'
        text = '老虎';
        pic1 = imread(strcat(path1,file_list1.tiger));
    case '25'
        text = '灰狼';
        pic1 = imread(strcat(path1,file_list1.wolf));    
    otherwise
        text = '错误';
        pic1 = 0;
        disp('错误');
end
switch holding
    case '1'
        text = '斧头';
        pic2 = imread(strcat(path2,file_list2.axe));
    case '2'
        text = '书包';
        pic2 = imread(strcat(path2,file_list2.backpack));
    case '3'
        text = '篮球';
        pic2 = imread(strcat(path2,file_list2.basketball));
    case '4'
        text = '书本';
        pic2 = imread(strcat(path2,file_list2.book));
    case '5'
        text = '花束';
        pic2 = imread(strcat(path2,file_list2.bouquet));
    case '6'
        text = '弓';
        pic2 = imread(strcat(path2,file_list2.bow));
    case '7'
        text = '大刀';
        pic2 = imread(strcat(path2,file_list2.broadsword));
    case '8'
        text = '画笔';
        pic2 = imread(strcat(path2,file_list2.brush));
    case '9'
        text = '蛋糕';
        pic2 = imread(strcat(path2,file_list2.cake));
    case '10'
        text = '橡皮擦';
        pic2 = imread(strcat(path2,file_list2.eraser));
    case '11'
        text = '钢笔';
        pic2 = imread(strcat(path2,file_list2.foutain_pen));
    case '12'
        text = '枪械';
        pic2 = imread(strcat(path2,file_list2.gun));
    case '13'
        text = '大槌';
        pic2 = imread(strcat(path2,file_list2.hammer));
    case '14'
        text = '长剑';
        pic2 = imread(strcat(path2,file_list2.long_sword));
    case '15'
        text = '手机';
        pic2 = imread(strcat(path2,file_list2.mobile_phone));
    case '16'
        text = '平底锅';
        pic2 = imread(strcat(path2,file_list2.pan));
    case '17'
        text = '铅笔';
        pic2 = imread(strcat(path2,file_list2.pencil));
    case '18'
        text = '尺子';
        pic2 = imread(strcat(path2,file_list2.ruler));
    case '19'
        text = '乒乓球拍';
        pic2 = imread(strcat(path2,file_list2.table_tennis_peddle));
    case '20'
        text = '雨伞';
        pic2 = imread(strcat(path2,file_list2.umberella));
    otherwise
        text = '错误';
        pic2 = 0;
        disp('错误');
end
end

中,读取图像时最容易出现"Reference to non-existent field"错误的可能是文件路径或文件名错误导致函数找不到需要读取的图像文件。建议在使用imread函数时,先检查一下文件路径和文件名是否正确。

在上述代码中,错误出现在strcat(path1,file_list1.bear)处,原因在于file_list1是一个dir函数返回的结构体数组,使用.操作符访问其中的属性,如file_list1(1).name,而不是file_list1.bear。修改代码如下所示:

function [text,pic1,pic2] = PictureRead(f1,f2)
animal = num2str(f1);
holding = num2str(f2);
path1 = 'E:/动物/';
path2 = 'E:/持有物/';
file_list1 = dir(fullfile(path1, '*.PNG'));
file_list2 = dir(fullfile(path2, '*.PNG'));
% 根据传入的参数选择对应的图片
switch animal
    case '1'
        text = '棕熊';
        pic1 = imread(fullfile(path1,file_list1(1).name)); 
        % 修改为fullfile函数,并使用()访问结构体中的属性
    case '2'
        text = '小猫';
        pic1 = imread(fullfile(path1,file_list1(2).name));
    case '3'
        text = '小鸡';
        pic1 = imread(fullfile(path1,file_list1(3).name));
    case '4'
        text = '小鹿';
        pic1 = imread(fullfile(path1,file_list1(4).name));
    case '5'
        text = '小牛';
        pic1 = imread(fullfile(path1,file_list1(5).name));
    case '6'
        % ...以此类推
end

以上修改应该能够解决问题。