效果如如下:
整个工程还是挺简单的,之前一直都是用matlab做信号处理,由于要做课程设计,就学了一下matlab的GUI。下面总结几个关键的地方。
(1)控件拉到自己喜欢的位置,并将控件的Text和Tag改好(不改也行,看个人习惯)
(2)设置一个全局变量global TextString,用来记录用户按了什么按钮,把字符串拼接起来
(3)做好每个按钮的回调函数的工作
(4)eval()函数能够把字符串转换成matlab命令并返回结果(用在‘=’的回调函数)
function varargout = untitled2(varargin)
% UNTITLED2 MATLAB code for untitled2.fig
% UNTITLED2, by itself, creates a new UNTITLED2 or raises the existing
% singleton*.
%
% H = UNTITLED2 returns the handle to a new UNTITLED2 or the handle to
% the existing singleton*.
%
% UNTITLED2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED2.M with the given input arguments.
%
% UNTITLED2('Property','Value',...) creates a new UNTITLED2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled2_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 untitled2
% Last Modified by GUIDE v2.5 09-Jul-2021 22:30:29
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled2_OpeningFcn, ...
'gui_OutputFcn', @untitled2_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 untitled2 is made visible.
function untitled2_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 untitled2 (see VARARGIN)
% Choose default command line output for untitled2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled2_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 window_Callback(hObject, eventdata, handles)
% hObject handle to window (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 window as text
% str2double(get(hObject,'String')) returns contents of window as a double
% --- Executes during object creation, after setting all properties.
function window_CreateFcn(hObject, eventdata, handles)
% hObject handle to window (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
global TextString
% --- Executes on button press in one.
function one_Callback(hObject, eventdata, handles)
% hObject handle to one (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.one,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in two.
function two_Callback(hObject, eventdata, handles)
% hObject handle to two (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.two,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in three.
function three_Callback(hObject, eventdata, handles)
% hObject handle to three (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.three,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in four.
function four_Callback(hObject, eventdata, handles)
% hObject handle to four (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.four,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in five.
function five_Callback(hObject, eventdata, handles)
% hObject handle to five (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.five,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in six.
function six_Callback(hObject, eventdata, handles)
% hObject handle to six (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.six,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in seven.
function seven_Callback(hObject, eventdata, handles)
% hObject handle to seven (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.seven,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in eight.
function eight_Callback(hObject, eventdata, handles)
% hObject handle to eight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.eight,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in nine.
function nine_Callback(hObject, eventdata, handles)
% hObject handle to nine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.nine,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in addition.
function addition_Callback(hObject, eventdata, handles)
% hObject handle to addition (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.addition,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in subtraction.
function subtraction_Callback(hObject, eventdata, handles)
% hObject handle to subtraction (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.subtraction,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in multiplication.
function multiplication_Callback(hObject, eventdata, handles)
% hObject handle to multiplication (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.multiplication,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject handle to division (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.division,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in sqrt.
function sqrt_Callback(hObject, eventdata, handles)
% hObject handle to sqrt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.sqrt,'String')
TextString = strcat(TextString , textString)
set(handles.window,'string',textString)
% --- Executes on button press in sin.
function sin_Callback(hObject, eventdata, handles)
% hObject handle to sin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.sin,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in cos.
function cos_Callback(hObject, eventdata, handles)
% hObject handle to cos (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.cos,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in tan.
function tan_Callback(hObject, eventdata, handles)
% hObject handle to tan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.tan,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in square.
function square_Callback(hObject, eventdata, handles)
% hObject handle to square (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.square,'String')
TextString = strcat(TextString ,'^2')
set(handles.window,'string',textString)
% --- Executes on button press in cot.
function cot_Callback(hObject, eventdata, handles)
% hObject handle to cot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.cot,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
% hObject handle to equal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
ans = eval(TextString)
a = strcat(TextString,'=')
aa = strcat(a,num2str(ans))
set(handles.window,'string',aa)
TextString = []
% --- Executes on button press in right.
function right_Callback(hObject, eventdata, handles)
% hObject handle to right (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.right,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in left.
function left_Callback(hObject, eventdata, handles)
% hObject handle to left (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.left,'String')
TextString = strcat(TextString ,textString)
set(handles.window,'string',textString)
% --- Executes on button press in qingkong.
function qingkong_Callback(hObject, eventdata, handles)
% hObject handle to qingkong (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TextString
TextString = []