用matlab编写代码实现在gui中打开图片
时间: 2024-02-24 09:56:38 浏览: 20
好的,下面是用Matlab编写代码实现在GUI中打开图片的方法:
1. 创建一个GUI窗口,在窗口中添加一个按钮和一个显示图片的Axes控件。
2. 在按钮的回调函数中添加以下代码:
```
[filename, pathname] = uigetfile({'*.jpg';'*.png';'*.bmp'}, '选择图片'); % 打开文件选择对话框
if isequal(filename,0) || isequal(pathname,0) % 判断用户是否选择了文件
return;
else
img = imread(fullfile(pathname, filename)); % 读取图片
imshow(img, 'Parent', handles.axes1); % 在Axes控件中显示图片
end
```
3. 在GUI的OpeningFcn函数中添加以下代码:
```
handles.output = hObject;
guidata(hObject, handles);
```
完整的代码如下:
```
function varargout = openimage(varargin)
% OPENIMAGE MATLAB code for openimage.fig
% OPENIMAGE, by itself, creates a new OPENIMAGE or raises the existing
% singleton*.
%
% H = OPENIMAGE returns the handle to a new OPENIMAGE or the handle to
% the existing singleton*.
%
% OPENIMAGE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in OPENIMAGE.M with the given input arguments.
%
% OPENIMAGE('Property','Value',...) creates a new OPENIMAGE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before openimage_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to openimage_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help openimage
% Last Modified by GUIDE v2.5 31-Jul-2021 22:56:06
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @openimage_OpeningFcn, ...
'gui_OutputFcn', @openimage_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before openimage is made visible.
function openimage_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to openimage (see VARARGIN)
% Choose default command line output for openimage
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes openimage wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = openimage_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg';'*.png';'*.bmp'}, '选择图片'); % 打开文件选择对话框
if isequal(filename,0) || isequal(pathname,0) % 判断用户是否选择了文件
return;
else
img = imread(fullfile(pathname, filename)); % 读取图片
imshow(img, 'Parent', handles.axes1); % 在Axes控件中显示图片
end
```
运行GUI,点击“打开图片”按钮就可以选择图片并在GUI中显示了。