matlab-GUI程序(3)

本文介绍了一元二次方程的数值解法,并通过GUI实现了方程求解器,包括输入参数设置、求解过程展示以及错误处理。

在打开函数中,增加handles中的字段

function test1_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 test1 (see VARARGIN)

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

% Update handles structure
handles.numa=1
handles.numb=1
handles.numc=1
guidata(hObject, handles);

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

 

 

下面完成将结果赋值

> help guihandles
 GUIHANDLES Return a structure of handles.
    HANDLES = GUIHANDLES(H) returns a structure containing handles of
    objects in a figure, using their tags as fieldnames.  Objects
    are excluded if their tags are empty, or are not legal variable
    names.  If several objects have the same tag, that field in the
    structure contains a vector of handles.  Objects with hidden
    handles are included in the structure.
 
    H is a handle that identifies the figure - it can be the figure
    itself, or any object contained in the figure.
 
    HANDLES = GUIHANDLES returns a structure of handles for the
    current figure.
 
    Example:
 
    Suppose an application creates a figure with handle F, containing
    a slider and an editable text uicontrol whose tags are 'valueSlider'
    and 'valueEdit' respectively.  The following excerpts from the
    application's M-file illustrate the use of GUIHANDLES in callbacks:
 
    ... excerpt from the GUI setup code ...
 
    f = figure;
    uicontrol('Style','slider','Tag','valueSlider', ...);
    uicontrol('Style','edit','Tag','valueEdit',...);
 
    ... excerpt from the slider's callback ...
 
    handles = guihandles(gcbo); % generate handles struct
    set(handles.valueEdit, 'string',...
        num2str(get(handles.valueSlider, 'value')));
 
    ... excerpt from the edit's callback ...
 
    handles = guihandles(gcbo);
    val = str2double(get(handles.valueEdit,'String'));
    if isnumeric(val) & length(val)==1 & ...
       val >= get(handles.valueSlider, 'Min') & ...
       val <= get(handles.valueSlider, 'Max')
      % update the slider's value if the edit's value is OK:
      set(handles.valueSlider, 'Value', val);
    else
      % flush the bad string out of the edit; replace with slider's
      % current value:
      set(handles.valueEdit, 'String',...
        num2str(get(handles.valueSlider, 'Value')));
    end
 
    Note that in this example, the structure of handles is created
    each time a callback executes.  See the GUIDATA help for an
    example in which the structure is created only once, and cached
    for subsequent use.

=================

matlab的一元高次方程数值解

 

 

 

 



 

 

计算结果的程序为:

function btn_draw_Callback(hObject, eventdata, handles)
% hObject    handle to btn_draw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%求解一元二次方程
myres=roots([handles.numa,handles.numb,handles.numc]);
set(handles.result,'string',mat2str(myres)) ;

 完整程序为

function varargout = test1(varargin)
% TEST1 M-file for test1.fig
%      TEST1, by itself, creates a new TEST1 or raises the existing
%      singleton*.
%
%      H = TEST1 returns the handle to a new TEST1 or the handle to
%      the existing singleton*.
%
%      TEST1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST1.M with the given input arguments.
%
%      TEST1('Property','Value',...) creates a new TEST1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test1_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 test1
                                            
% Last Modified by GUIDE v2.5 04-Oct-2012 17:21:13

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

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

% Update handles structure
handles.numa=1
handles.numb=1
handles.numc=1
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = test1_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 btn_draw.
function btn_draw_Callback(hObject, eventdata, handles)
% hObject    handle to btn_draw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%求解一元二次方程
myres=roots([handles.numa,handles.numb,handles.numc]);
set(handles.result,'string',mat2str(myres)) ;

function a_num_Callback(hObject, eventdata, handles)
% hObject    handle to a_num (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 a_num as text
%        str2double(get(hObject,'String')) returns contents of a_num as a double
numa=str2double(get(hObject,'string'));
if isnan(numa)
      errordlg('请输入数字','输入错误','modal');
end
handles.numa=numa;
guidata(hObject, handles);

      

% --- Executes during object creation, after setting all properties.
function a_num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to a_num (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



function b_num_Callback(hObject, eventdata, handles)
% hObject    handle to b_num (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 b_num as text
%        str2double(get(hObject,'String')) returns contents of b_num as a double
numb=str2double(get(hObject,'string'));
if isnan(numb)
      errordlg('请输入数字','输入错误','modal');
end
handles.numb=numb;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function b_num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to b_num (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



function c_num_Callback(hObject, eventdata, handles)
% hObject    handle to c_num (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 c_num as text
%        str2double(get(hObject,'String')) returns contents of c_num as a double
numc=str2double(get(hObject,'string'));
if isnan(numc)
      errordlg('请输入数字','输入错误','modal');
end
handles.numc=numc;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function c_num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to c_num (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

 用到的主要知识为:

1、get,set获取和设置属性

2、GUI上的控件,都在handles结构中,比如如果有个edit1的文本编辑框,则在M程序中表示为handles.edit1

3、使用guidata来更新GUI的共享数据,可用于控件之间交换数据,此外要加进handles的结构要在打开函数中增加

4、errordlg显示错误提示框

5、关于gco

 

>> help gco
 GCO Get handle to current object.
    OBJECT = GCO returns the current object in the current figure.
 
    OBJECT = GCO(FIG) returns the current object in the figure FIG.
 
    The current object is the last object clicked on, excluding
    uimenus.  If the click was not over a figure child, the
    figure itself will be the current object.
 
    The handle of the current object is stored in the figure
    property CurrentObject, and can be accessed directly using GET
    and SET.
 
    Use GCO in a callback to obtain the handle of the object that
    was clicked on.  MATLAB updates the current object before
    executing each callback, so the current object may change if
    one callback is interrupted by another.  To obtain the right
    handle during a callback, get the current object early, before
    any calls to DRAWNOW, WAITFOR, PAUSE, FIGURE, or GETFRAME which
    provide opportunities for other callbacks to interrupt.
 
    If no figures exist, GCO returns [].

 

MatlabGUI里面启动或者暂停Simulink模型-start_and_stop.mdl 针对这个问题:https://www.ilovematlab.cn/thread-23233-1-1.html 现在我做一个集中解答,从mathworks那里学习了一下。 第一步:创建你自己的Simulink模型,这个不用我讲吧,我们使用以下模型做演示: start_and_stop.mdl 第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_and_stop_gui.fig start_and_stop_gui.m Figure12.jpg 在start simulation里面,直接用sim函数,这个大家好像都会: % --- Executes on button press in startsim. function startsim_Callback % hObject handle to startsim % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data modelname = get; stoptime = str2num); sim复制代码 在stop simulation,里面,callback这样写: % --- Executes on button press in stopsim. function stopsim_Callback % hObject handle to stopsim % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data global GUIStopFlag; GUIStopFlag = 1; 复制代码第三步:写一个s函数,不停地检测这个GUIStopFlag参数,发送命令给STOP按钮,告诉他,可以结束了,简单吧? sysstop_new.m 第四步:在simulink模型里面,加上这个S函数 Figure13.jpg 好了,大功告成了,现在你就可以在GUI里随意控制Simulink模型了。 注:文中所用的模型,GUI文件,由mathworks公司提供。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值