我之前问过一个问题,是关于在函数调用中保持一个持久变量,我想知道是否可以对对象做类似的事情。在函数调用中保持一个对象的持久性
例如,如果我有这样的一段代码:
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会记得的第一个对象我打电话。
我花了一些时间玩弄调试器和回声函数来弄清楚如何在结构中存储对象的工作,但一旦我得到它,一切工作完美,谢谢。 – Amit
真棒!乐意效劳。 –