www.gusucode.com > 演示了GUI界面的布局和效果源码程序 > 新建文件夹/ex2.m

    function varargout = ex2(varargin)
% EX2 M-file for ex2.fig
%      EX2, by itself, creates a new EX2 or raises the existing
%      singleton*.
%
%      H = EX2 returns the handle to a new EX2 or the handle to
%      the existing singleton*.
%
%      EX2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EX2.M with the given input arguments.
%
%      EX2('Property','Value',...) creates a new EX2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ex2_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ex2_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 ex2

% Last Modified by GUIDE v2.5 15-Mar-2008 15:06:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ex2_OpeningFcn, ...
                   'gui_OutputFcn',  @ex2_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 ex2 is made visible.
function ex2_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 ex2 (see VARARGIN)

% Choose default command line output for ex2
handles.output = hObject;
web -browser http://www.ilovematlab.cn/thread-3976-1-1.html
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ex2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%make sure the simulink model is opened 
if  isempty(find_system('Name','GUI_ex11')),
	open_system('GUI_ex11'); 
	figure(handles.figure1)
end

%initial the GUI
set(handles.edit1,'String',0);
set(handles.slider1,'Value',0);
set(handles.text_y1,'String','y1');
set(handles.text_y2,'String','y2');
set(handles.text_y3,'String','y3');


% --- Outputs from this function are returned to the command line.
function varargout = ex2_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set_param('GUI_ex11/pushbutton_value1','Value','1');
set_param('GUI_ex11/pushbutton_value2','Value','0');
%in order to see the change of GUI directly
sim('GUI_ex11');
update_GUI(handles);


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, 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)
set_param('GUI_ex11/pushbutton_value1','Value','0');
set_param('GUI_ex11/pushbutton_value2','Value','-1');
%in order to see the change of GUI directly
sim('GUI_ex11');
update_GUI(handles);


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
OldVal = get(handles.slider1,'Value');
NewStrVal = get(handles.edit1,'String');
NewVal = str2double(NewStrVal);
if isempty(NewVal)|(NewVal<0)|(NewVal>50)
    set(hObject,'string',OldVal);
else
    set(handles.slider1,'Value',NewVal);
end
handles.edit1_slider1_value = get(handles.edit1,'String');
guidata(hObject,handles);
set_param('GUI_ex11/edit1_slider1_value','Value',handles.edit1_slider1_value);
%in order to see the change of GUI directly
sim('GUI_ex11');
update_GUI(handles);


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
NewVal = get(hObject,'Value');
set(handles.edit1,'String',num2str(NewVal));

%set the param of model edit1_slider1_value,be careful that the constant
%Value must a string
set_param('GUI_ex11/edit1_slider1_value','Value',num2str(NewVal));
%in order to see the change of GUI directly
sim('GUI_ex11');
update_GUI(handles);

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in pushbutton_simulink.
function pushbutton_simulink_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_simulink (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%make sure the simulink model is opened 
if  isempty(find_system('Name','GUI_ex11')),
	open_system('GUI_ex11'); 
	figure(handles.figure1)
end


%run the simulink model
sim('GUI_ex11');


function update_GUI(handles)
%get the value of y
handles.y1 = evalin('base','y1');
handles.y2 = evalin('base','y2');
handles.y3 = evalin('base','y3');


%send the value to GUI static texts
set(handles.text_y1,'String',handles.y1);
set(handles.text_y2,'String',handles.y2);
set(handles.text_y3,'String',handles.y3);



% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (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 togglebutton1
handles.togglebuttonValue = get(handles.togglebutton1,'Value');
if handles.togglebuttonValue == 1
    set_param('GUI_ex11/togglebutton_value','Value','10');
else
    set_param('GUI_ex11/togglebutton_value','Value','0');
end
%in order to see the change of GUI directly
sim('GUI_ex11');
update_GUI(handles);