失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > matlab-GUI编写简单计算器

matlab-GUI编写简单计算器

时间:2019-09-12 19:04:24

相关推荐

matlab-GUI编写简单计算器

GUI编写简单计算器*加减乘除,以及部分特殊符号,其实原理特别简单;

语法结构包括实现原理很多大佬都有详细的说明了,这里就不赘述了,简单的入门计算器。

掌握好几个简单的语法就好:

1)获取:变量名=get(handles,‘属性名’)

设置:set(handles,‘属性名’,变量名)

2)使用handles结构在控件之间传递数据。

使用guihandles获得与传入的句柄相关联的handles结构,从而对不同GUI的控件进行操作。

我自己做的这个布局十分简单,大家就不要嘲笑了,这里直接贴上源码。另外附加了一个加菜单项&&菜单项的功能实现

先看下做完后的效果;

function varargout = calculator(varargin)% CALCULATOR MATLAB code for calculator.fig%CALCULATOR, by itself, creates a new CALCULATOR or raises the existing%singleton*.%%H = CALCULATOR returns the handle to a new CALCULATOR or the handle to%the existing singleton*.%%CALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in CALCULATOR.M with the given input arguments.%%CALCULATOR('Property','Value',...) creates a new CALCULATOR or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before calculator_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to calculator_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 calculator% Last Modified by GUIDE v2.5 25-Apr- 21:29:03% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @calculator_OpeningFcn, ...'gui_OutputFcn', @calculator_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before calculator is made visible.function calculator_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 calculator (see VARARGIN)set(handles.edit1,'string','');set(handles.edit2,'string','');% Choose default command line output for calculatorhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes calculator wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = calculator_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 structurevarargout{1} = handles.output;% --- 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.edit1,'String');textString = strcat(textString,'4');set(handles.edit1,'String',textString);guidata(hObject, handles);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');endfunction edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (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 edit2 as text% str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (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.edit1,'String');textString = strcat(textString,'7');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (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.edit1,'String');textString = strcat(textString,'8');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'9');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5 (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.edit1,'String');textString = strcat(textString,'5');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'6');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'1');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'2');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'3');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'*');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'/');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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)textString = get(handles.edit1,'String');textString = strcat(textString,'-');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'.');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'+');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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.edit1,'String');aa = eval(textString);set(handles.edit2,'String',aa);guidata(hObject, handles);% --- 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)close(gcf);% --- 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.edit1,'String');as=char(textString);n=length(textString);textString=as(1:n-1);set(handles.edit1,'String',textString)guidata(hObject, handles);% --- 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)textString = get(handles.edit1,'String');textString = get(handles.edit2,'String');set(handles.edit1,'String','');set(handles.edit2,'String','');guidata(hObject, handles);% --- 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.edit1,'String');textString = strcat(textString,'0');set(handles.edit1,'String',textString);guidata(hObject, handles);% --- 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)textString = get(handles.edit1,"String");textString = strcat(textString,'√');set(handles.edit1,'String',textString);guidata(hObject, handles);function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (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 edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a doubleI=imread('C:\Users\Gerry\Desktop\test\1.jpg');%读取玫瑰花图片subplot(2,2,3);imshow(I);hold on [x1,y1]=ginput(1);[x2,y2]=ginput(1);hold on plot(x1,y1,'*');hold onplot(x2,y2,'*');Get4=imcrop(I,[x1,y1,abs(x1-x2),abs(y1-y2)]);subplot(2,2,4);imshow(Get4);% --- Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (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');endfunction edit4_Callback(hObject, eventdata, handles)% hObject handle to edit4 (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 edit4 as text% str2double(get(hObject,'String')) returns contents of edit4 as a double% --- Executes during object creation, after setting all properties.function edit4_CreateFcn(hObject, eventdata, handles)% hObject handle to edit4 (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 open.function open_Callback(hObject, eventdata, handles)% hObject handle to open (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)I=imread('C:\Users\Gerry\Desktop\test\1.jpg');subplot(2,2,3);imshow(I);% --- Executes on button press in save.function save_Callback(hObject, eventdata, handles)% hObject handle to save (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)saveas(gcf, 'C:\Users\Gerry\Desktop\test\get.jpg', 'jpg');% --- Executes on button press in change.function change_Callback(hObject, eventdata, handles)i=imread('C:\Users\Gerry\Desktop\test\1.jpg');figure;imshow(i);hold on;[x,y]=ginput(2);for m=1:2;plot(x(m),y(m),'*');endn=imcrop(i,[min(x(1),x(2)),min(y(1),y(2)),abs(x(1)-x(2)),abs(y(1)-y(2))]);figure;imshow(n);imwrite(n,'C:\Users\Gerry\Desktop\test\get1.jpg')% --------------------------------------------------------------------function openjpg_Callback(hObject, eventdata, handles)% hObject handle to openjpg (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)I=imread('C:\Users\Gerry\Desktop\test\1.jpg');subplot(2,2,3);imshow(I);% --------------------------------------------------------------------function Change_Callback(hObject, eventdata, handles)% hObject handle to Change (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)i=imread('C:\Users\Gerry\Desktop\test\1.jpg');figure;imshow(i);hold on;[x,y]=ginput(2);for m=1:2;plot(x(m),y(m),'*');endn=imcrop(i,[min(x(1),x(2)),min(y(1),y(2)),abs(x(1)-x(2)),abs(y(1)-y(2))]);figure;imshow(n);imwrite(n,'C:\Users\Gerry\Desktop\test\tset4.jpg')% --------------------------------------------------------------------function Untitled_5_Callback(hObject, eventdata, handles)% hObject handle to Untitled_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function changejpg_Callback(hObject, eventdata, handles)% hObject handle to changejpg (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function rose_Callback(hObject, eventdata, handles)% hObject handle to rose (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)I=imread('C:\Users\Gerry\Desktop\test\1.jpg');subplot(2,2,3);imshow(I);% --------------------------------------------------------------------function get_Callback(hObject, eventdata, handles)% hObject handle to get (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)i=imread('C:\Users\Gerry\Desktop\test\1.jpg');figure;imshow(i);hold on;[x,y]=ginput(2);for m=1:2;plot(x(m),y(m),'*');endn=imcrop(i,[min(x(1),x(2)),min(y(1),y(2)),abs(x(1)-x(2)),abs(y(1)-y(2))]);figure;imshow(n);imwrite(n,'C:\Users\Gerry\Desktop\test\tset4.jpg');% --------------------------------------------------------------------function Untitled_10_Callback(hObject, eventdata, handles)% hObject handle to Untitled_10 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_11_Callback(hObject, eventdata, handles)% hObject handle to Untitled_11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)saveas(gcf, 'C:\Users\Gerry\Desktop\test\Get', 'jpg');% --- Executes during object creation, after setting all properties.function sqrt_CreateFcn(hObject, eventdata, handles)% hObject handle to sqrt (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called

如果觉得《matlab-GUI编写简单计算器》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。