声明:关于变量传递问题使用的是handles和guidata函数。
首先创建一个gui 空白界面:
打开第一个,可以看到界面
创建一个按钮,一个切换按钮,一个列表框
假设我要完成的事件是这样的:
按钮为赋值变量num为1
第一次摁下切换按钮负责对num进行+2循环操作,并将每次循环num
值打在列表框内,且保留原有数据
再次摁下切换按钮,终止循环
首先保存GUI界面,得到.m文件:
查看按钮回调函数,写入代码:
查看切换按钮回调函数,写入代码:
目前已经从按钮的函数中获得了num的数值,切换按钮的基本框架也已经架构完毕
下一步进行num以及终止循环,补充代码如下:
下一步,想列表框传送数据,打开列表框回调函数:
列表框名称为listbox2,打开切换按钮,补充代码为:
点击运行就可以啦。
下附完整函数:
function varargout = untitled1(varargin)
% UNTITLED1 MATLAB code for untitled1.fig
% UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing
% singleton*.
%
% H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle to
% the existing singleton*.
%
% UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED1.M with the given input arguments.
%
% UNTITLED1('Property','Value',...) creates a new UNTITLED1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled1
% Last Modified by GUIDE v2.5 01-Jan-2019 21:00:11
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled1_OpeningFcn, ...
'gui_OutputFcn', @untitled1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled1 is made visible.
function untitled1_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 untitled1 (see VARARGIN)
% Choose default command line output for untitled1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
num = 1; %定义num函数值
handles.num = num; %num赋值给结构体
guidata(hObject, handles); %更新handles结构体
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in togglebutton2.
function togglebutton2_Callback(hObject, eventdata, handles)
num = handles.num; %从结构体接收num值
global STOP; %定义全局变量的行为在复杂工程内非常危险
button_state = get(hObject,'Value');%获取切换按钮状态
if button_state == 1 %如果第一次按下
a = {}; %定义一个空数组
STOP = 0;
while num>0
if STOP == 0
a = [a;num];%数组维度增加1
%向列表框写入数组
set(handles.listbox2,'string',a)
%计算下一次所需维度
index = size(get(handles.listbox2,'string'), 1);
%列表框升维
set(handles.listbox2,'Value',index);
%以上升维操作是因为列表框和数组维度相同才可以赋值
pause(0.5); %延时函数,便于观察
num = num+1;
else
break;
end
end
end
if button_state == 0 %如果第二次按下
STOP = 1;
end
% hObject handle to togglebutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton2
% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox2
% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end