您的位置:首页 > 产品设计 > UI/UE

GUI数据传递userdata

2013-11-27 15:24 309 查看
function varargout = lsss(varargin)
% LSSS MATLAB code for lsss.fig
%      LSSS, by itself, creates a new LSSS or raises the existing
%      singleton*.
%
%      H = LSSS returns the handle to a new LSSS or the handle to
%      the existing singleton*.
%
%      LSSS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LSSS.M with the given input arguments.
%
%      LSSS('Property','Value',...) creates a new LSSS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before lsss_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to lsss_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 lsss

% Last Modified by GUIDE v2.5 27-Nov-2013 10:44:06

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

% Choose default command line output for lsss
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = lsss_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;

xlabel('t')
ylabel('y')

% --- 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)
tspan = [0 get(handles.time, 'Userdata')]; %这里就是用Userdata传递数据
y0 = [get(handles.y0, 'Userdata');get(handles.dy0, 'Userdata')];
options = odeset('RelTol', 1e-4, 'AbsTol', [1e-4 1e-4]);
[t, y] = ode45(@fun, tspan, y0, options);
plot(t, y(:, 1), '-')

syms y(t)
Dy = diff(y);
fu = dsolve(diff(y, 2) == -6*cos(2*t)+3*sin(t)+t-11.5, y(0) == y0(1), Dy(0) == y0(1));
t2 = 0:tspan(2)/100:tspan(2);
yAna = subs(fu, t, t2);
hold on
plot(t2, yAna, 'r-')
legend('numerical solution', 'analytical solution')
title('y=-6*cos(2*t)+3*sin(t)+t-11.5')
hold off

function dy = fun(t, y)
    dy = zeros(2, 1);
    dy(1) = y(2);
    dy(2) = -6*cos(2*t)+3*sin(t)+t-11.5;

function y0_Callback(hObject, eventdata, handles)
% hObject    handle to y0 (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 y0 as text
%        str2double(get(hObject,'String')) returns contents of y0 as a double
y0 = str2double(get(hObject, 'String'));
set(hObject, 'UserData', y0)

% --- Executes during object creation, after setting all properties.
function y0_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y0 (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
y0 = str2double(get(hObject, 'String'));
set(hObject, 'UserData', y0)

function dy0_Callback(hObject, eventdata, handles)
% hObject    handle to dy0 (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 dy0 as text
%        str2double(get(hObject,'String')) returns contents of dy0 as a double
dy0 = str2double(get(hObject, 'String'));
set(hObject, 'UserData', dy0)

% --- Executes during object creation, after setting all properties.
function dy0_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dy0 (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
dy0 = str2double(get(hObject, 'String'));
set(hObject, 'UserData', dy0)

% --- Executes on slider movement.
function time_Callback(hObject, eventdata, handles)
% hObject    handle to time (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
tspan = get(hObject,'Value');
set(hObject, 'UserData', tspan);

% --- Executes during object creation, after setting all properties.
function time_CreateFcn(hObject, eventdata, handles)
% hObject    handle to time (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
tspan = get(hObject,'Value');
set(hObject, 'UserData', tspan);


结果:



参考:

【1】GUI间的数据传递机制http://blog.csdn.net/dfd1r/article/details/6302063
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: