【问题标题】:How to display an image selected using PushButton in Matlab如何在 Matlab 中显示使用 PushButton 选择的图像
【发布时间】:2015-06-02 13:37:33
【问题描述】:

我正在使用 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

有什么建议吗?

提前谢谢你。

【问题讨论】:

  • 是handles.axesimage 还是handles.myaxesimage?错误消息和您的代码不匹配。此外,在使用 imshow 之前,您似乎需要调用 imread。
  • @Benoit_11 它的“handles.axesimage”

标签: image matlab user-interface


【解决方案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 中使用任何变量时

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-04
    • 2018-07-05
    • 2020-09-06
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    相关资源
    最近更新 更多