0.銷售訂單管理系統
0.1 功能:
- 打開檔案
- 查詢訂單
- 插入訂單
- 訂單統計
- 系統退出
0.2 資料庫:
- 資料檔案Excel
0.3 字段:
- 編号(E111)
- 客戶(王小明)
- 金額(23453.33)
1.系統設計
1.1 查詢訂單頁面
查詢條件:訂單編号
輸出内容:客戶姓名、金額
1.2 插入訂單頁面
輸入内容:訂單編号、客戶姓名、金額
按鈕内容:确定、重置、傳回
1.3 訂單統計頁面
輸出内容:訂單總額、大寫金額(元)、訂單交易數(個)
按鈕内容:查詢、重置、傳回
2.分析設計準備
2.1 如何實作系統功能
2.1.1 子產品Module
- 組成系統的基本單元
- 由内部屬性和外部屬性組成
- 内部屬性:算法(函數體)、内部變量(局部變量)
- 外部屬性:功能(函數名)、輸入輸出資料(參數)
2.2 Matlab存取Excel檔案資料
函數:
- xlsfinfo()
- [data, textdata] = xlsread(filename,sheet,range)
- [status,message] = xlswrite(filename,sheet,range)
2.3 Matlab Graphics User Interface
練習
- 向execl中寫入檔案,金額轉換成大寫。
-
完整代碼第一部分:向execl中寫入檔案
頁面如下(頁面中的讀取函數在第一部分的代碼中未寫):
Test.m:
關鍵代碼:
% --- 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)
global num txt
[num,txt] = xlsread('YH.xlsx');
numy = get(handles.edit1,'string');
num2 = {numy};
namey = get(handles.edit2,'string');
name2 = {namey};
money = get(handles.edit3,'string');
% money2 = {money};
money2 = str2num(money);
len = size(txt,1)+1;
range = num2str(len);
if isempty(numy)==1
msgbox('訂單編号為空,請重新輸入');
end
if isempty(namey)==1
msgbox('客戶姓名為空,請重新輸入');
end
if isempty(money)==1
msgbox('金額為空,請重新輸入');
end
if isempty(numy)==0 && isempty(namey)==0 && isempty(money2)==0
xlswrite('YH.xlsx',num2,'sheet1',['B',range]);
xlswrite('YH.xlsx',name2,'sheet1',['A',range]);
xlswrite('YH.xlsx',money2,'sheet1',['C',range]);
msgbox('插入成功');
end
完整代碼(冗長可略過):
function varargout = Test(varargin)
% TEST MATLAB code for Test.fig
% TEST, by itself, creates a new TEST or raises the existing
% singleton*.
%
% H = TEST returns the handle to a new TEST or the handle to
% the existing singleton*.
%
% TEST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST.M with the given input arguments.
%
% TEST('Property','Value',...) creates a new TEST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Test_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Test_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 Test
% Last Modified by GUIDE v2.5 22-Nov-2019 11:25:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Test_OpeningFcn, ...
'gui_OutputFcn', @Test_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 Test is made visible.
function Test_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 Test (see VARARGIN)
% Choose default command line output for Test
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Test wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Test_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
function 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)
global num txt
[num,txt] = xlsread('YH.xlsx');
numy = get(handles.edit1,'string');
num2 = {numy};
namey = get(handles.edit2,'string');
name2 = {namey};
money = get(handles.edit3,'string');
% money2 = {money};
money2 = str2num(money);
len = size(txt,1)+1;
range = num2str(len);
if isempty(numy)==1
msgbox('訂單編号為空,請重新輸入');
end
if isempty(namey)==1
msgbox('客戶姓名為空,請重新輸入');
end
if isempty(money)==1
msgbox('金額為空,請重新輸入');
end
if isempty(numy)==0 && isempty(namey)==0 && isempty(money2)==0
xlswrite('YH.xlsx',num2,'sheet1',['B',range]);
xlswrite('YH.xlsx',name2,'sheet1',['A',range]);
xlswrite('YH.xlsx',money2,'sheet1',['C',range]);
msgbox('插入成功');
end
% --- 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)
a1 = get(handles.edit1,'string');
set(handles.edit2,'string',strcat('def',a1));
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 double
% --- 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');
end
% --------------------------------------------------------------------
function file_Callback(hObject, eventdata, handles)
% hObject handle to file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function edit_Callback(hObject, eventdata, handles)
% hObject handle to edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function tools_Callback(hObject, eventdata, handles)
% hObject handle to tools (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function help_Callback(hObject, eventdata, handles)
% hObject handle to help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function new_Callback(hObject, eventdata, handles)
% hObject handle to new (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
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)
% --------------------------------------------------------------------
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)
% --------------------------------------------------------------------
function Untitled_8_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function close_Callback(hObject, eventdata, handles)
% hObject handle to close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)