读取图片时, 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

js-type">text = '小狗'; pic1 = imread(strcat(path1,file_list1.dog)); case '6' text = '小老鼠'; pic1 = imread(strcat(path1,file_list1.mouse)); end switch holding case '1' pic2 = imread(strcat(path2,file_list2.honey))); case '2' pic2 = imread(strcat(path2,file_list2.leaf))); case '3' pic2 = imread(strcat(path2,file_list2.iphone))); case '4' pic2 = imread(strcat(path2,file_list2.pen))); case '5' pic2 = imread(strcat(path2,file_list2.book))); end end

参考资料: - imread函数文档 - MATLAB central关于"Referenec to non-existent field"错误的解释

答案:

根据代码,问题应该出在读取图片的语句上。在switch语句中,对应的图片文件名应该是一个字符串(如'bear.PNG'),但代码中使用了类似于结构体的语法,使用了类似于访问结构体属性的方法(如file_list1.bear)。这样的语法在matlab中是不合法的,因此会出现"Reference to non-existent field"的错误。

解决方法是,将switch语句中读取图片的语句修改为使用字符串拼接的形式,如imread(strcat(path1,'bear.PNG'))。同时,需要检查所要读取的图片文件名与实际存在的文件名是否完全一致,包括大小写、扩展名等。

修改后的代码如下:

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,'bear.PNG'));
        case '2'
            text = '小猫';
            pic1 = imread(strcat(path1,'cat.PNG'));
        case '3'
            text = '小鸡';
            pic1 = imread(strcat(path1,'chicken.PNG'));
        case '4'
            text = '小鹿';
            pic1 = imread(strcat(path1,'corpus.PNG'));
        case '5'
            text = '小狗';
            pic1 = imread(strcat(path1,'dog.PNG'));
        case '6'
            text = '小老鼠';
            pic1 = imread(strcat(path1,'mouse.PNG'));
    end
    switch holding
        case '1'
            pic2 = imread(strcat(path2,'honey.PNG'));
        case '2'
            pic2 = imread(strcat(path2,'leaf.PNG'));
        case '3'
            pic2 = imread(strcat(path2,'iphone.PNG'));
        case '4'
            pic2 = imread(strcat(path2,'pen.PNG'));
        case '5'
            pic2 = imread(strcat(path2,'book.PNG'));
    end
end