本文实现了mlapp的简易计算器实现,并打包了app;
classdef calculator < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure CEButton matlab.ui.control.Button inputTextArea matlab.ui.control.TextArea inputTextAreaLabel matlab.ui.control.Label Button_21 matlab.ui.control.Button ResultsTextArea matlab.ui.control.TextArea ResultsTextAreaLabel matlab.ui.control.Label Button_20 matlab.ui.control.Button Button_14 matlab.ui.control.Button Button_15 matlab.ui.control.Button Button_16 matlab.ui.control.Button Button_17 matlab.ui.control.Button Button_13 matlab.ui.control.Button Button_12 matlab.ui.control.Button Button_11 matlab.ui.control.Button Button_19 matlab.ui.control.Button Button_18 matlab.ui.control.Button Button_10 matlab.ui.control.Button Button_9 matlab.ui.control.Button Button_3 matlab.ui.control.Button Button_8 matlab.ui.control.Button Button_7 matlab.ui.control.Button Button_6 matlab.ui.control.Button Button_5 matlab.ui.control.Button Button_4 matlab.ui.control.Button Button_2 matlab.ui.control.Button Button matlab.ui.control.Button end % Callbacks that handle component events methods (Access = private) % Button pushed function: Button_15 function Button_15Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'0'); end end % Button pushed function: Button function ButtonPushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='1'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'1'); end end % Button pushed function: Button_2 function Button_2Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='2'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'2'); end end % Button pushed function: Button_3 function Button_3Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='3'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'3'); end end % Button pushed function: Button_4 function Button_4Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='4'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'4'); end end % Button pushed function: Button_5 function Button_5Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='5'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'5'); end end % Button pushed function: Button_6 function Button_6Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='6'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'6'); end end % Button pushed function: Button_7 function Button_7Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='7'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'7'); end end % Button pushed function: Button_8 function Button_8Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='8'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'8'); end end % Button pushed function: Button_9 function Button_9Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='9'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'9'); end end % Button pushed function: Button_16 function Button_16Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='pi'; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'pi'); end end % Button pushed function: Button_14 function Button_14Pushed(app, event) app.inputTextArea.Value=strcat(app.inputTextArea.Value,'.'); end % Button pushed function: Button_10 function Button_10Pushed(app, event) app.inputTextArea.Value=strcat(app.inputTextArea.Value,'+'); end % Button pushed function: Button_18 function Button_18Pushed(app, event) app.inputTextArea.Value=strcat(app.inputTextArea.Value,'-'); end % Button pushed function: Button_19 function Button_19Pushed(app, event) app.inputTextArea.Value=strcat(app.inputTextArea.Value,'*'); end % Button pushed function: Button_20 function Button_20Pushed(app, event) app.inputTextArea.Value=strcat(app.inputTextArea.Value,'/'); end % Button pushed function: Button_11 function Button_11Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); if(numbers_mat=='0') app.inputTextArea.Value='('; else app.inputTextArea.Value=strcat(app.inputTextArea.Value,'('); end end % Button pushed function: Button_12 function Button_12Pushed(app, event) app.inputTextArea.Value=strcat(app.inputTextArea.Value,')'); end % Button pushed function: Button_21 function Button_21Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); x=mat2str(numbers_mat); y1=eval(numbers_mat); y=sprintf('%.10f',y1); app.ResultsTextArea.Value=y; app.inputTextArea.Value='0'; end % Button pushed function: CEButton function CEButtonPushed(app, event) app.inputTextArea.Value='0'; end % Button pushed function: Button_13 function Button_13Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); numbers_mat_2=strcat('0-',numbers_mat); y1=eval(numbers_mat_2); y=sprintf('%.10f',y1); app.ResultsTextArea.Value=y; app.inputTextArea.Value='0'; end % Button pushed function: Button_17 function Button_17Pushed(app, event) numbers_mat=cell2mat(app.inputTextArea.Value); numbers_mat_2=strcat('100*',numbers_mat); y1=eval(numbers_mat_2); y=sprintf('%.10f',y1); app.ResultsTextArea.Value=strcat(y,'%'); app.inputTextArea.Value='0'; end end % Component initialization methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure and hide until all components are created app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 640 480]; app.UIFigure.Name = 'MATLAB App'; % Create Button app.Button = uibutton(app.UIFigure, 'push'); app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true); app.Button.Position = [174 306 60 22]; app.Button.Text = '1'; % Create Button_2 app.Button_2 = uibutton(app.UIFigure, 'push'); app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true); app.Button_2.Position = [261 306 60 22]; app.Button_2.Text = '2'; % Create Button_4 app.Button_4 = uibutton(app.UIFigure, 'push'); app.Button_4.ButtonPushedFcn = createCallbackFcn(app, @Button_4Pushed, true); app.Button_4.Position = [174 243 60 24]; app.Button_4.Text = '4'; % Create Button_5 app.Button_5 = uibutton(app.UIFigure, 'push'); app.Button_5.ButtonPushedFcn = createCallbackFcn(app, @Button_5Pushed, true); app.Button_5.Position = [261 244 60 22]; app.Button_5.Text = '5'; % Create Button_6 app.Button_6 = uibutton(app.UIFigure, 'push'); app.Button_6.ButtonPushedFcn = createCallbackFcn(app, @Button_6Pushed, true); app.Button_6.Position = [354 244 60 22]; app.Button_6.Text = '6'; % Create Button_7 app.Button_7 = uibutton(app.UIFigure, 'push'); app.Button_7.ButtonPushedFcn = createCallbackFcn(app, @Button_7Pushed, true); app.Button_7.Position = [174 172 60 26]; app.Button_7.Text = '7'; % Create Button_8 app.Button_8 = uibutton(app.UIFigure, 'push'); app.Button_8.ButtonPushedFcn = createCallbackFcn(app, @Button_8Pushed, true); app.Button_8.Position = [261 174 60 22]; app.Button_8.Text = '8'; % Create Button_3 app.Button_3 = uibutton(app.UIFigure, 'push'); app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true); app.Button_3.Position = [355 306 60 22]; app.Button_3.Text = '3'; % Create Button_9 app.Button_9 = uibutton(app.UIFigure, 'push'); app.Button_9.ButtonPushedFcn = createCallbackFcn(app, @Button_9Pushed, true); app.Button_9.Position = [355 174 60 22]; app.Button_9.Text = '9'; % Create Button_10 app.Button_10 = uibutton(app.UIFigure, 'push'); app.Button_10.ButtonPushedFcn = createCallbackFcn(app, @Button_10Pushed, true); app.Button_10.Position = [448 306 60 22]; app.Button_10.Text = '+'; % Create Button_18 app.Button_18 = uibutton(app.UIFigure, 'push'); app.Button_18.ButtonPushedFcn = createCallbackFcn(app, @Button_18Pushed, true); app.Button_18.Position = [448 244 60 22]; app.Button_18.Text = '-'; % Create Button_19 app.Button_19 = uibutton(app.UIFigure, 'push'); app.Button_19.ButtonPushedFcn = createCallbackFcn(app, @Button_19Pushed, true); app.Button_19.Position = [448 174 60 22]; app.Button_19.Text = '*'; % Create Button_11 app.Button_11 = uibutton(app.UIFigure, 'push'); app.Button_11.ButtonPushedFcn = createCallbackFcn(app, @Button_11Pushed, true); app.Button_11.Position = [76 303 66 25]; app.Button_11.Text = '('; % Create Button_12 app.Button_12 = uibutton(app.UIFigure, 'push'); app.Button_12.ButtonPushedFcn = createCallbackFcn(app, @Button_12Pushed, true); app.Button_12.Position = [75 240 66 26]; app.Button_12.Text = ' )'; % Create Button_13 app.Button_13 = uibutton(app.UIFigure, 'push'); app.Button_13.ButtonPushedFcn = createCallbackFcn(app, @Button_13Pushed, true); app.Button_13.Position = [75 174 66 22]; app.Button_13.Text = '±'; % Create Button_17 app.Button_17 = uibutton(app.UIFigure, 'push'); app.Button_17.ButtonPushedFcn = createCallbackFcn(app, @Button_17Pushed, true); app.Button_17.Position = [174 107 60 22]; app.Button_17.Text = '%'; % Create Button_16 app.Button_16 = uibutton(app.UIFigure, 'push'); app.Button_16.ButtonPushedFcn = createCallbackFcn(app, @Button_16Pushed, true); app.Button_16.Position = [75 106 66 22]; app.Button_16.Text = 'π'; % Create Button_15 app.Button_15 = uibutton(app.UIFigure, 'push'); app.Button_15.ButtonPushedFcn = createCallbackFcn(app, @Button_15Pushed, true); app.Button_15.Position = [261 107 60 22]; app.Button_15.Text = '0'; % Create Button_14 app.Button_14 = uibutton(app.UIFigure, 'push'); app.Button_14.ButtonPushedFcn = createCallbackFcn(app, @Button_14Pushed, true); app.Button_14.Position = [355 107 60 22]; app.Button_14.Text = '.'; % Create Button_20 app.Button_20 = uibutton(app.UIFigure, 'push'); app.Button_20.ButtonPushedFcn = createCallbackFcn(app, @Button_20Pushed, true); app.Button_20.Position = [448 107 60 22]; app.Button_20.Text = '/'; % Create ResultsTextAreaLabel app.ResultsTextAreaLabel = uilabel(app.UIFigure); app.ResultsTextAreaLabel.HorizontalAlignment = 'right'; app.ResultsTextAreaLabel.Position = [388 425 46 22]; app.ResultsTextAreaLabel.Text = 'Results'; % Create ResultsTextArea app.ResultsTextArea = uitextarea(app.UIFigure); app.ResultsTextArea.Position = [449 369 152 80]; % Create Button_21 app.Button_21 = uibutton(app.UIFigure, 'push'); app.Button_21.ButtonPushedFcn = createCallbackFcn(app, @Button_21Pushed, true); app.Button_21.Position = [534 106 67 222]; app.Button_21.Text = '='; % Create inputTextAreaLabel app.inputTextAreaLabel = uilabel(app.UIFigure); app.inputTextAreaLabel.HorizontalAlignment = 'right'; app.inputTextAreaLabel.Position = [158 423 31 22]; app.inputTextAreaLabel.Text = 'input'; % Create inputTextArea app.inputTextArea = uitextarea(app.UIFigure); app.inputTextArea.Position = [204 367 152 80]; % Create CEButton app.CEButton = uibutton(app.UIFigure, 'push'); app.CEButton.ButtonPushedFcn = createCallbackFcn(app, @CEButtonPushed, true); app.CEButton.Position = [75 370 66 75]; app.CEButton.Text = 'CE'; % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end % App creation and deletion methods (Access = public) % Construct app function app = calculator % Create UIFigure and components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) if nargout == 0 clear app end end % Code that executes before app deletion function delete(app) % Delete UIFigure when app is deleted delete(app.UIFigure) end end endView Code
实现界面:
参考文献:
https://wenku.baidu.com/view/2b3e8644f41fb7360b4c2e3f5727a5e9846a27fa.html