2015-04-20 57 views
0

我之前問過一個問題,是關於在函數調用中保持一個持久變量,我想知道是否可以對對象做類似的事情。在函數調用中保持一個對象的持久性

例如,如果我有這樣的一段代碼:

function Gui(backofplayingcard,store,card) 
v = 1; 
w = 1200; 
h = 550; 
f = figure('visible', 'off','color','white','position',[1 1 w h]); 

movegui(f,'center'); 

set(f,'visible','on','name','matching game'); 



    % create 6 by 6 grid with a picture above a pushbutton for each location 
    % on the grid 

    for p = 1:6 
    for w = 1:6 

    subplot(6,6,((p - 1) * 6 + w)); 
    imshow(backofplayingcard); 

    button(v) = uicontrol('Style','pushbutton','value',"some value dependent on p and w",'String','Flip!','Position',... 
[[(152 * (w)) + ((w) * 10) + 25] [((7 - p) * 69) - ((p) * 10) + 33] 60 20],... 
'Callback',{@flip}); 
v = v + 1; 

end 
end 
end 

會有對我來說,存儲的第一個對象我有手柄稱爲方式。所以 例如:

 function flip(hObject,eventdata) 
     persistent a; 
     if isempty(a) 
     handle1 = hObject; 
     a = 1; 
     else 
     check1 = get(handle1,'value'); 
     check2 = get(hObject,'value'); 

    if check1 == check2 

     disp('hello'); 

    else 
     disp('goodbye'); 

    end 
    end 
    end 

這樣MATLAB會記得的第一個對象我打電話。

回答

3

正如我對你以前的問題的回答不太好,我建議使用句柄結構來存儲回調間共享的數據。

對於這種情況,我想自定義標記添加到您的循環中創建的每個按鈕,這將可以很容易地知道哪個按鈕被按下,並以什麼順序。例如,假設標籤在循環中被創建爲如下形式:

sprintf('Button %i',(p - 1) * 6 + w)) 

基本上每個按鈕都會標有它的編號。如果需要,您可以將其更改爲簡單號碼。

爲了獲得按下的所有按鈕的列表,您可以在flip函數中簡單地將它們連接成垂直連接,所以您將有一個單元格數組(在我的示例中,但可能是一個數值數組,取決於標記您選擇)包含按下的每個按鈕的Tag。我還添加了一個處理結構的計數器,以防萬一你想知道有多少按鈕被按下...

我並不是故意使用這個,但是在GUI的句柄結構中存儲變量是安全的方式來確保每個回調都能方便地訪問它們。

所以這裏是你的代碼,增加了一些。你可以隨心所欲調整它。我加了

%// ===== NEW ===== 

指向我添加的新東西。

您可以將此代碼複製/粘貼爲一個新的.m文件,並查看每次按下按鈕時看起來像什麼handles.ListFlip。在命令窗口中

function CardGui 

clc 
clear 

v = 1; 
w = 1200; 
h = 550; 

%// Demo image 
backofplayingcard = imread('coins.png'); 

f = figure('visible', 'off','color','white','position',[1 1 w h]); 

movegui(f,'center'); 

set(f,'visible','on','name','matching game'); 

%// ==== NEW ==== 
%// Initialize cell array containing list of buttons and counter. 
handles.ListFlip = cell(1,1); 
handles.flipCounter = 0; 
%// ============= 

% create 6 by 6 grid with a picture above a pushbutton for each location 
% on the grid 

for p = 1:6 
    for w = 1:6 

     subplot(6,6,((p - 1) * 6 + w)); 
     imshow(backofplayingcard); 

     %// ==== NEW ==== 
     %// Added the buttons to the handles structure and gave them tags 
     handles.button(v) = uicontrol('Style','pushbutton','value',0,'String','Flip!','Position',... 
      [(152 * (w)) + ((w) * 10) + 25 ((7 - p) * 69) - ((p) * 10) + 33 60 20],... 
      'Callback',{@flip},'Tag',sprintf('Button %i',(p - 1) * 6 + w)); 
     v = v + 1; 

    end 
end 

guidata(f,handles); 

    function flip(hObject,~) 

     handles = guidata(f); 

     %// ==== NEW ==== 
     %// Get current button selected (using its tag) 
     CurrentButton = get(hObject,'Tag'); 

     %// Add it to the list 

     if handles.flipCounter == 0 
     handles.ListFlip = CurrentButton; 
     else 
     handles.ListFlip = [handles.ListFlip;CurrentButton]; 
     end 

     handles.flipCounter = handles.flipCounter +1; 
     %// ============= 

     guidata(f,handles) 
    end 
end 

樣本輸出後,我按下3個按鈕:

Button 1 
Button 3 
Button 6 

希望有所幫助。您可以將此代碼與您收到的上一個問題的答案結合起來,並且所有代碼都應該正常工作。

+0

我花了一些時間玩弄調試器和回聲函數來弄清楚如何在結構中存儲對象的工作,但一旦我得到它,一切工作完美,謝謝。 – Amit

+0

真棒!樂意效勞。 –

相關問題