(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)
filename是一个文本文件。 如何使用PUSHBUTTON显示相应的图像?
答案 0 :(得分:1)
这是因为你弄乱了GUIDE生成的代码。 通常回调函数定义如下所示:
function SomeButton_Callback(hObject, eventdata, handles)
但是在你的代码中你写了
function open1_Callback(filename, hObject, eventdata, handles)
但是指南仍然按照特定的顺序向回调函数(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
关闭。当你无法在你最喜欢的编辑器中更新文件时,你会学到很多东西,因为其他程序正在使用它。
另见(Making universal variables in 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
。