MATLAB GUI程式打包成.exe的方法流程和注意事項
Matlab GUI程式打包成.exe的功能,對Matlab GUI程式而言,無疑是一種解脫。
比如,重在演算法設計和資料測試的自動駕駛ADAS感知系統。
流程
第一步:開啟編譯工具
在Matlab命令視窗,輸入 deploytool函式。
deploytool
出現application Compiler 視窗,如下圖
第二步:設定編譯資訊
在這裡,可以設定多類資訊:
A GUI程式資訊
畢竟要為GUI程式,打包成.exe檔案。需要GUI程式資訊,包括GUI主程式,
B 軟體資訊
目標.exe檔案的封面、作者聯絡資訊、軟體功能描述
C GUI程式的支援包
這是非常重要的支援資訊。Matlab工具箱集合非常強大,便於程式開發,但是在打包過程中,也就需要把支援包,都要打包附帶。
D 設定目標.exe檔案打包後的儲存路徑
E 軟體執行所依賴的所有檔案和資料夾
第三步:設定打包方式
這裡打包方式,取決於Matlab Runtime的來源設定。
A 來自Web
打包速度快,體積小。使用者得到.exe檔案後,安裝時,需要下載Matlab runtime,佔用時間。
B 打包者,下載,併入到打包後的檔案中
打包速度慢,但是使用者根據簡單提示,就能安裝使用.exe檔案。
最後,點選Package,執行打包過程
執行完成之後,得到系統提示。
1 for_redistribution
2 ----MyAppInstaller_web.exe
3 for_redistribution_files_only
4 ----myCameraGUI.exe
5 ----readme.txt
6 ----splash.png
7 for_testing
8 ----mccExcludedFiles.log
9 ----myCameraGUI.exe
10 ----readme.txt
11 ----requiredMCRProducts.txt
12 ----splash.png
這是打包輸出後的檔案目錄。
當用戶電腦有對應版本的Matlab Runtime時,只需要執行 for_testing 資料夾中的檔案。
如果沒有,就需要for_redistribution資料夾中的檔案,安裝Matlab Runtime。
案例
第一個:基於USB鏡頭的實時影像獲取與顯示
編譯方式: Web式打包
原始碼:
1 function varargout = myCameraGUI(varargin)
2 gui_Singleton = 1;
3 gui_State = struct('gui_Name', mfilename, ...
4 'gui_Singleton', gui_Singleton, ...
5 'gui_OpeningFcn', @myCameraGUI_OpeningFcn, ...
6 'gui_OutputFcn', @myCameraGUI_OutputFcn, ...
7 'gui_LayoutFcn', [] , ...
8 'gui_Callback', []);
9 if nargin && ischar(varargin{1})
10 gui_State.gui_Callback = str2func(varargin{1});
11 end
12
13 if nargout
14 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
15 else
16 gui_mainfcn(gui_State, varargin{:});
17 end
18
19 function myCameraGUI_OpeningFcn(hObject, eventdata, handles, varargin)
20
21 handles.output = hObject;
22
23 handles.video = videoinput('winvideo', 1);
24 set(handles.video,'TimerPeriod', 0.05, ...
25 'TimerFcn',['if(~isempty(gco)),'...
26 'handles=guidata(gcf);'... % Update handles
27 'image(getsnapshot(handles.video));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
28 'set(handles.cameraAxes,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
29 'else '...
30 'delete(imaqfind);'... % Clean up - delete any image acquisition objects
31 'end']);
32 triggerconfig(handles.video,'manual');
33 handles.video.FramesPerTrigger = Inf; % Capture frames until we manually stop it
34
35 % Update handles structure
36 guidata(hObject, handles);
37
38 % UIWAIT makes mycameragui wait for user response (see UIRESUME)
39 uiwait(handles.myCameraGUI);
40
41
42 % --- Outputs from this function are returned to the command line.
43 function varargout = myCameraGUI_OutputFcn(hObject, eventdata, handles)
44 handles.output = hObject;
45 varargout{1} = handles.output;
46
47
48 % --- Executes on button press in startStopCamera.
49 function startStopCamera_Callback(hObject, eventdata, handles)
50 % Start/Stop Camera
51 if strcmp(get(handles.startStopCamera,'String'),'Start Camera')
52 % Camera is off. Change button string and start camera.
53 set(handles.startStopCamera,'String','Stop Camera')
54 start(handles.video)
55 set(handles.startAcquisition,'Enable','on');
56 set(handles.captureImage,'Enable','on');
57
58 else
59 % Camera is on. Stop camera and change button string.
60 set(handles.startStopCamera,'String','Start Camera')
61 stop(handles.video)
62 set(handles.startAcquisition,'Enable','off');
63 set(handles.captureImage,'Enable','off');
64 end
65
66 % --- Executes on button press in captureImage.
67 function captureImage_Callback(hObject, eventdata, handles)
68
69 frame = get(get(handles.cameraAxes,'children'),'cdata'); % The current displayed frame
70 save('testframe.mat', 'frame');
71 disp('Frame saved to file ''testframe.mat''');
72
73
74 % --- Executes on button press in startAcquisition.
75 function startAcquisition_Callback(hObject, eventdata, handles)
76 if strcmp(get(handles.startAcquisition,'String'),'Start Acquisition')
77 % Camera is not acquiring. Change button string and start acquisition.
78 set(handles.startAcquisition,'String','Stop Acquisition');
79 trigger(handles.video);
80 else
81 % Camera is acquiring. Stop acquisition, save video data,
82 % and change button string.
83 stop(handles.video);
84 disp('Saving captured video...');
85
86 videodata = getdata(handles.video);
87 save('testvideo.mat', 'videodata');
88 disp('Video saved to file ''testvideo.mat''');
89
90 start(handles.video); % Restart the camera
91 set(handles.startAcquisition,'String','Start Acquisition');
92 end
93
94 % --- Executes when user attempts to close myCameraGUI.
95 function myCameraGUI_CloseRequestFcn(hObject, eventdata, handles)
96
97 delete(hObject);
98 delete(imaqfind);
第二個:Matlab計算器
編譯方式:整合式打包
calculator本身只有2.5m,程式碼可能只有6k.但是打包後的檔案,達到850M。
這是M語言的弊端。類似的狀況有LabVIEW語言程式設計,想要打包成.exe,也產生一個體積巨大的安裝包。
1 function varargout = calculator(varargin)
2
3 gui_Singleton = 1;
4 gui_State = struct('gui_Name', mfilename, ...
5 'gui_Singleton', gui_Singleton, ...
6 'gui_OpeningFcn', @calculator_OpeningFcn, ...
7 'gui_OutputFcn', @calculator_OutputFcn, ...
8 'gui_LayoutFcn', [] , ...
9 'gui_Callback', []);
10 if nargin && ischar(varargin{1})
11 gui_State.gui_Callback = str2func(varargin{1});
12 end
13
14 if nargout
15 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
16 else
17 gui_mainfcn(gui_State, varargin{:});
18 end
19
20 function calculator_OpeningFcn(hObject, eventdata, handles, varargin)
21
22 handles.output = hObject;
23
24 % Update handles structure
25 guidata(hObject, handles);
26
27 % --- Outputs from this function are returned to the command line.
28 function varargout = calculator_OutputFcn(hObject, eventdata, handles)
29
30 varargout{1} = handles.output;
31
32
33
34 function edit1_Callback(hObject, eventdata, handles)
35
36 input1 = str2num(get(hObject,'String'));
37 if (isempty(input1))
38 set(hObject,'String','0');
39 end
40 guidata(hObject,handles);
41
42
43 % --- Executes during object creation, after setting all properties.
44 function edit1_CreateFcn(hObject, eventdata, handles)
45
46 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
47 set(hObject,'BackgroundColor','white');
48 end
49
50
51 function edit2_Callback(hObject, eventdata, handles)
52
53 input2 = str2num(get(hObject,'String'));
54 if (isempty(input2))
55 set(hObject,'String','0');
56 end
57 guidata(hObject,handles);
58
59
60 % --- Executes during object creation, after setting all properties.
61 function edit2_CreateFcn(hObject, eventdata, handles)
62 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
63 set(hObject,'BackgroundColor','white');
64 end
65
66
67
68 function answer_Callback(hObject, eventdata, handles)
69
70 function answer_CreateFcn(hObject, eventdata, handles)
71
72 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
73 set(hObject,'BackgroundColor','white');
74 end
75
76
77 % --- Executes on button press in AddPushbutton.
78 function AddPushbutton_Callback(hObject, eventdata, handles)
79
80 input111 = get(handles.edit1,'String');
81 input222 = get(handles.edit2,'String');
82 total = str2num(input111) + str2num(input222)
83 c = num2str(total)
84
85 set(handles.answer,'String',c);
86 guidata(hObject,handles);
第三個:自動駕駛感知系統
https://u.wechat.com/MFl-TPiUT-Xb0P2U7aOxIZo(二維碼自動識別)
智慧USV實時感知系統是一套用於自動駕駛裝備(無人車、船)智慧感知的應用系統。其中,感測器硬體主要包括:camera * 6、stereo camera * 2、radar * 1、GPS、IMU、4G 通訊。主要功能:實現對自動駕駛裝備的架勢環境進行實時感知,以輔助自動駕駛的控制決策,具體包括:GPS定位、車牌目標識別、汽車識別、雷達探測。目標屬性有:ID、定位、距離、相對方位。
軟體自2020.04.28開發。歡迎使用者朋友,聯絡[email protected]反饋意見,共同優化。
這個部分,就不提供程式碼了。歡迎交流。
注意事項
1.Matlab Runtime的下載
說實話,我現在還是沒有安裝獨立的Matlab Runtime。在解決這個問題的過程中,花費了一天的時間,到最後還是沒有解決。但是,通過其他方式,避免了Matlab Runtime的下載。
補充一下:Matlab Runtime已經成功下載。
2.編譯時間
整合式打包時間,非常長。第一次編譯,需要耐心。
3. 涉及到硬體的GUI程式的打包
目前,僅僅涉及到多個USB硬體的程式,打包後,安裝使用過程,沒有出現出錯。其他同學,如果遇到問題,可以在此留言。