这个是我在书上看到的
%保存为shili33.m
h0=figure('toolbar','none','position',[198 56 350 300]);
x=0:0.5:2*pi;
y=sin(x);
h=plot(x,y);
grid on
huidiao=[...
'if i==1,',...
'i=0;,',...
'y=cos(x);,',...
'delete(h),',...
'set(hm,"string","正弦函数"),',...
'h=plot(x,y);,',...
'grid on,',...
'else if i==0,',...
'i=1;,',...
'y=sin(x);,',...
'set(hm,"string","余弦函数"),',...
'delete(h),',...
'h=plot(x,y);,',...
'grid on,',...
'end,',...
'end'];
hm=uicontrol(gcf,'style','pushbutton',...
'string','余弦函数',...
'callback',huidiao);
i=1;
set(hm,'position',[250 20 60 20]);
set(gca,'position',[0.2 0.2 0.6 0.6]);
title('按钮的使用')
hold on;
下面这个是我用guide新建的,但我只是把模样做出来,并没有把任何功能实现
function varargout = temp(varargin)
% TEMP M-file for temp.fig
% TEMP, by itself, creates a new TEMP or raises the existing
% singleton*.
%
% H = TEMP returns the handle to a new TEMP or the handle to
% the existing singleton*.
%
% TEMP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEMP.M with the given input arguments.
%
% TEMP('Property','Value',...) creates a new TEMP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before temp_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to temp_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
% Copyright 2002-2003 The MathWorks, Inc.
% Edit the above text to modify the response to help temp
% Last Modified by GUIDE v2.5 09-Jul-2007 10:32:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @temp_OpeningFcn, ...
'gui_OutputFcn', @temp_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 temp is made visible.
function temp_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 temp (see VARARGIN)
% Choose default command line output for temp
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes temp wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = temp_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)
我想知道这两种方法有什么区别,如何把第一种的功能用第二种方法实现。
还有就是为什么第一程序无法正常运行的?