MATLAB 程序设计基础之gui设计
掌握Matlab图形用户界面的设计流程
用matlab设计有图形界面的计算器程序,其中清空和退出功能使用菜单栏实现(例子:calculator1)
先打开matlab新建gui用户界面,构思好自己的计算机界面后右键单击查看回调函数,补充相应功能
1.相应功能:基本加减乘除操作,使用菜单栏编辑退出键和清零键
button
和静态文本框组合,在静态文本框显示输入输出结果string
栏以显示想要的数字及运算符 textString = get(handles.text1,'String');
textString = strcat(textString,'0');
set(handles.text1,'String',textString)
0~9方法相似,将’0’修改为其他相应数字即可
textString = get(handles.text1,'String');
textString = strcat(textString,'+');
set(handles.text1,'String',textString)
textString = get(handles.text1,'String');
ans=eval(textString);
set(handles.text1,'String',ans)
close(gcf)
set(handles.text1,'String',' ')
1.熟练掌握gui图形界面设计
2. 主要函数:
figure=:创建新图形窗口对象 ; set: 设置图形对象各属性 ;
get: 获取图形对象各属性
设置:set(handles.控件的tag,‘要设置的属性名’,‘要设置的属性值’)。比如:set(handles.edit1,‘string’,'hello world’);
获取:get(handles.控件的tag,‘要获取的属性名’)
函数参数:hObject :当前figure的句柄;
eventdata:为了兼容将来版本的保留接口,直接忽略;
handles:结构体,包含figure中所有对象句柄,初始化结束后, 所有初始数据保存到变量handles中
function varargout = text1(varargin)
% TEXT1 MATLAB code for text1.fig
% TEXT1, by itself, creates a new TEXT1 or raises the existing
% singleton*.
%
% H = TEXT1 returns the handle to a new TEXT1 or the handle to
% the existing singleton*.
%
% TEXT1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEXT1.M with the given input arguments.
%
% TEXT1('Property','Value',...) creates a new TEXT1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before text1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to text1_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 text1
% Last Modified by GUIDE v2.5 18-Jun-2019 22:54:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @text1_OpeningFcn, ...
'gui_OutputFcn', @text1_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 text1 is made visible.
function text1_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 text1 (see VARARGIN)
% Choose default command line output for text1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes text1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = text1_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;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- 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)
textString = get(handles.text1,'String');
textString = strcat(textString,'0');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'2');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'1');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'3');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'4');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'5');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'6');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'7');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'8');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'9');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.text1,'String',' ')
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
ans=eval(textString);
set(handles.text1,'String',ans)
% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton15 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'+');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'-');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton17 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'*');
set(handles.text1,'String',textString)
% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton18 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'/');
set(handles.text1,'String',textString)
% --- Executes during object creation, after setting all properties.
function text1_CreateFcn(hObject, eventdata, handles)
% hObject handle to text1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton19 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton20 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(gcf)
% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(gcf)
% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.text1,'String','')
% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton21 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString = strcat(textString,'.');
set(handles.text1,'String',textString)