2015-04-20 26 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

真棒!乐意效劳。 –