Re: [問題] GUI錯誤訊息已回收

看板MATLAB作者 (溫柔殺手N￾ ￾ ￾  )時間17年前 (2008/11/01 23:11), 編輯推噓5(5016)
留言21則, 4人參與, 最新討論串2/2 (看更多)
小弟向正在看小弟程式的版眾,深深一鞠躬<(_._)> 感謝您的幫忙, 不過小弟已經找到程式的問題出在哪裡了 問題就出在...要命的好習慣! clear; close; clc; 這三行.... 我把她們註解掉就可以了!機車! 但是此時小弟就有另外一個疑問.... 因為以前寫程式都要確保不會有上次執行的變數以及結果留下來,都會加這三行。 是不是在GUI方面的撰寫是不需要的? 如果仍舊是需要的話,請問應該要加在哪邊比較恰當呢? 小弟也是看了5.6個小時了吧!眼睛都快脫窗了。 在一次感謝正在看小弟程式的版眾,在一次向您鞠躬致謝<(_._)> : 主要程式碼: : function GUIDraw_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 GUIDraw (see VARARGIN) : % Choose default command line output for GUIDraw : handles.output = hObject; : % Update handles structure : guidata(hObject, handles); : % UIWAIT makes GUIDraw wait for user response (see UIRESUME) : % uiwait(handles.figure1); : clear; : close; : clc; : [x,y,z]=peaks; : mesh(x,y,z); : xlabel('X-Axis'); : ylabel('Y-Axis'); : zlabel('Z-Axis'); : title('peaks'); : % Set the Accelerator : set (handles.ym,'Accelerator','y'); : set (handles.mm,'Accelerator','m'); : set (handles.cm,'Accelerator','c'); : set (handles.rm,'Accelerator','r'); : set (handles.gm,'Accelerator','g'); : set (handles.bm,'Accelerator','b'); : set (handles.wm,'Accelerator','w'); : set (handles.km,'Accelerator','k'); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.216.178 ※ 編輯: yimean 來自: 61.230.216.178 (11/01 23:18)

11/01 23:52, , 1F
我的習慣只要是function都不會加上這3個指令喔
11/01 23:52, 1F

11/02 07:23, , 2F
function body的一開始其實根本不必加clear,因為call一個
11/02 07:23, 2F

11/02 07:25, , 3F
function,就進入一個新的workspace。
11/02 07:25, 3F

11/02 07:28, , 4F
function call return後,該workspace就被自動清除了。
11/02 07:28, 4F

11/02 07:31, , 5F
通常在寫MATLAB scripts的時候才需要手clear base workspace
11/02 07:31, 5F

11/02 07:32, , 6F
11/02 07:32, 6F

11/02 07:36, , 7F
不過如果你在function call中想手動清掉function workspace也
11/02 07:36, 7F

11/02 07:37, , 8F
的variables也不是不行。只是最好要指定你要清的variables。
11/02 07:37, 8F

11/02 07:39, , 9F
否則你下只下個clear;指令,function workspace所有的
11/02 07:39, 9F

11/02 07:39, , 10F
variables都被清掉。
11/02 07:39, 10F

11/02 07:42, , 11F
像原po會出錯的原因似乎是因為他把guide產生的variables也清
11/02 07:42, 11F

11/02 07:42, , 12F
掉的原故。就是handles.output = hObject;那一行。
11/02 07:42, 12F

11/02 07:47, , 13F
其實MATLAB的function workspaces就像是C/C++的function
11/02 07:47, 13F

11/02 07:47, , 14F
scopes。
11/02 07:47, 14F

11/02 11:14, , 15F
這真的算好習慣嗎? XD
11/02 11:14, 15F

11/02 14:59, , 16F
樓上,在寫scripts比較有用。如果一個script不clear舊的
11/02 14:59, 16F

11/02 15:01, , 17F
variables,再run一次該script就有可能會出現錯誤。通常發生
11/02 15:01, 17F

11/02 15:02, , 18F
在上一次run的結果產生了一個較大的array,但這一次run的結果
11/02 15:02, 18F

11/02 15:05, , 19F
只用到一個較小的array。那多餘的部分是有可能不小心被這次
11/02 15:05, 19F

11/02 15:06, , 20F
的執行存取到的。
11/02 15:06, 20F

11/03 00:33, , 21F
感謝諸位熱心的高手回應以及提供意見...<(_._)>
11/03 00:33, 21F
文章代碼(AID): #19376wau (MATLAB)
討論串 (同標題文章)
文章代碼(AID): #19376wau (MATLAB)