0

http://s1273.photobucket.com/user/Chethan_tv/media/CBIR_zpsb48bce14.jpg.html

上图是我的最终输出,open按钮用于查看显示在各个轴上的图像。我使用以下代码进行显示

function open1_Callback(filename, hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fid=fopen(filename);
ax = handles.(sprintf('axes%d', 6));
imshow(ax)

其中,6 是轴数。但是我遇到了错误,例如 undefined variable handles 在我使用以下代码的轴上显示图像

function displayResults(filename,hObject, eventdata, handles)

% Open 'filename' file... for reading...
fid = fopen(filename);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);  
end
guidata(hObject,handles)

文件名是一个文本文件。如何使用按钮显示相应的图像?

4

1 回答 1

1

这是因为您弄乱了 GUIDE 生成的代码。通常回调函数定义如下所示:

function SomeButton_Callback(hObject, eventdata, handles)

但是在你写的代码中

function open1_Callback(filename, hObject, eventdata, handles)

但是 guide 仍然以这个特定的顺序向回调函数( 、 和 )hObject发送eventdata三个参数。handles所以 MatLab 会感到困惑并抛出错误。

您最好将文件名放入函数handles结构中*_OpeningFcn,然后在所有回调中使用它。

在您的末尾,您*_OpeningFcn应该添加以下内容:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);

然后在按钮的回调函数中

function open1_Callback(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% open the file
fid=fopen(handles.MyData.ListFileName);
% load lines from the file
% and do what is needed
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);  
end
% dont' forget to close the file
fclose(fid);
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);

是的,你用函数打开的所有文件都fopen应该用fclose. 当您因为其他程序正在使用它而无法在您最喜欢的编辑器中更新您的文件时,您将很难学会。

另请参阅(在 MATLAB GUI 中制作通用变量

更新以反映评论中的讨论:

为了实现您想要的行为,我会执行以下操作:

在您的末尾*_OpeningFcn添加以下内容:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images    = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);

    % store file name and image itself for later use
    handles.MyData.Images{N} = rgb;
    handles.MyData.FileNames{N} = imagename;

    % we have buttons like open1 open2 open3 etc...
    % add some info to the open buttons
    % so they will be aware which image they display
    btn = handles.(sprintf('open%d', N));
    set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);

然后在打开按钮的回调函数中执行以下操作

function open1_Callback(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

N   = get(hObject,'UserData');
rgb = handles.MyData.Images{N};
ax  = handles.(sprintf('axes%d', N));
% create figure in 'modal' mode, so user have to close it to continue
figure('WindowStyle','modal', 'Name',handles.MyData.FileNames{N} );
% show image
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);

基本上,这个回调是通用的:它应该无需修改所有打开的按钮即可工作。您甚至可以将 GUIDE 中open2, open3... 按钮的回调函数更改为open1_Callback.

于 2013-05-11T05:30:17.243 回答