(代码: 2\global_data.m)
% 窗体打开前,先执行该函数function global_data_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject;% (1)第一种方式% global times;% times=0;% guidata(hObject, handles); 更新数据% (2)第二种方式% handles.times=0;% guidata(hObject, handles);% (3)第三种方式% set(handles.add,'UserData',0); %利用add按钮(句柄)的 UserData 属性来传递数据。% guidata(hObject, handles);% (4)第四种方式setappdata(handles.figure1,'times',0); %利用setappdata函数来设置全局变量guidata(hObject, handles);% add按钮的回调函数function add_Callback(hObject, eventdata, handles)times=getappdata(handles.figure1,'times'); %获取全局变量的值times=times+1;setappdata(handles.figure1,'times',times); %更新全局变量的值set(handles.disp,'String',num2str(times)); %设置disp文本框的显示内容