MATLAB GUI编程显示载入.JPG图片

MATLAB GUI软件界面显示载入的.JPG图片分两种情况,一种是希望点击按钮后显示载入的图片、另一种情况是希望软件界面一开始就载入默认位置的图片。 启动MATLAB 2018b后,在命令行光标后输入: guide 并回车,选择并打开名为 untitled8.fig 的文件。我从互联网上下载了NVIDIA和AMD两个游戏显卡公司的图片作为素材,分别用好压看图2345调整完像素大小并保存为NVIDIA.jpg和AMD.jpg

一、启动时默认载入NVIDIA.jpg并显示在GUI轴 axes3

找到对象“axes3”,查看回调 CreatFcn 因为有代码语句: % Hint: place code in OpeningFcn to populate axes3 所以了解到,需要把载入和显示图片的代码插入到这个GUI图像的 OpeningFcn函数下面位置。 % --- Executes just before untitled8 is made visible.

function untitled8_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 untitled8 (see VARARGIN)

% Choose default command line output for untitled8

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes untitled8 wait for user response (see UIRESUME)

% uiwait(handles.figure1);

im = imread(NVIDIA.jpg);

axes(handles.axes3) %在坐标轴3下画图

imshow(im)

二、点击按钮后再显示载入的图片 找到按钮对象,查看回调Callback

% --- 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)

warning off %取消警告

im = imread(AMD.jpg);%读图

axes(handles.axes2) %在坐标轴2下画图

imshow(im) %显示图像

经验分享 程序员 微信小程序 职场和发展