如何在Matlab中显示使用PushButton选择的图像

时间:2015-03-29 15:24:25

标签: image matlab user-interface

我正在使用Matlab-2012a中的基本GUI。我想显示选择并使用按钮显示图像。

这是我的代码:

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TrainingData;
global filenames;

TrainingData={};

[filenames, pathname] = uigetfile({'*.jpg';'*.png';'*.bmp'});

if ~ischar(filenames) % on cancel press you display a message of error with errordlg
errordlg('Error!','No file selected'); % displays an error message by means of errordlg function
 return;
end

axes(handles.myaxesImage);
imshow(filenames);

我可以浏览图片,但我无法显示相同内容。我收到以下错误消息:

Reference to non-existent field 'axesImage'.

Error in GUI>pushbutton2_Callback (line 93)
axes(handles.axesImage);

Error in gui_mainfcn (line 96)
    feval(varargin{:});

Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)GUI('pushbutton2_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

有什么建议吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

根据您的代码

  

轴(handles.myaxesImage);

MATLAB说

  

引用不存在的字段' axesImage'

因此MATLAB识别对象 handles.myaxesImage

存在问题

因为它不存在!

这是一个例子

在我的GUI的.m文件中

我想在标签为axes1

的轴上显示图像



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

%== GUI get file in the folder ==%
[FileName,PathName] = uigetfile({'*.tif';'*.jpg';'*.png'},'Select a image file');

%== Create a object handles.img to load image ==%
handles.img  = imread(FileName);  

%== call axes whose tag is axes1 to show image ==%
axes(handles.axes1);
imshow(handles.img);

guidata(hObject, handles); 




下次你想在GUI中使用任何varable

不要忘记检查该对象是否存在

希望有所帮助