functionbackswitch_Callback(hObject, eventdata, handles)% hObject handle to backswitch(see GCBO)% eventdata reserved - to be defined in a future version ofMATLAB% handles structure with handles and user data(see GUIDATA)
ha =axes('units','normalized','position',[0011]);uistack(ha,'down')[fname, pname, index]=uigetfile({'*.jpg; *.bmp'},'选择背景图片');if index
II=imread([pname,fname]);image(II)
colormap gray
set(ha,'handlevisibility','off','visible','off');elseerrordlg('选择背景失败');%提示错误对话框
hDialog =findall(0,'tag','Msgbox_Error Dialog');
btn_ok =findall(hDialog,'style','pushbutton');set(btn_ok,'String','确定');
end
1.2 通过GUI运行设置背景颜色
%--- Executes just before computesumprice is made visible.functioncomputesumprice_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 ofMATLAB% handles structure with handles and user data(see GUIDATA)% varargin command line arguments to computesumprice(see VARARGIN)% Choose default command line output for computesumprice
ha =axes('units','normalized','position',[0011]);uistack(ha,'down')% up,down,可选
II=imread('1.jpg');image(II)
colormap gray
set(ha,'handlevisibility','off','visible','off');
handles.output = hObject;% Update handles structure
guidata(hObject, handles);
2. 错误对话框
2.1 “截屏失败”举例
%--- Executes on button press in screencapture.functionscreencapture_Callback(hObject, eventdata, handles)% hObject handle to screencapture(see GCBO)% eventdata reserved - to be defined in a future version ofMATLAB% handles structure with handles and user data(see GUIDATA)[fname,~, index]=uiputfile({'*.jpg'});if index
a =getframe(computesumprice);%注意不是gcf
imwrite(a.cdata,fname);elseerrordlg('导出失败');%提示错误对话框
hDialog =findall(0,'tag','Msgbox_Error Dialog');
btn_ok =findall(hDialog,'style','pushbutton');set(btn_ok,'String','确定');
end
3. 计算器
按钮callback
str_cur =get(handles.display,'string');
str_cur_val =str2double(get(handles.display,'string'));current(1)=get(hObject,'string');% 判断初始化是否为0if str_cur_val ==0set(handles.display,'string',current(1));elseset(handles.display,'string',[str_cur,current(1)]);
end