how do i write a function which recognize user-defined functions and plots the results in my gui
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
[EDIT: Fri May 27 01:13:10 UTC 2011 - Reformat - MKF]
function varargout = untitled78(varargin)
% UNTITLED78 M-file for untitled78.fig
% UNTITLED78, by itself, creates a new UNTITLED78 or raises the existing
% singleton*.
%
% H = UNTITLED78 returns the handle to a new UNTITLED78 or the handle to
% the existing singleton*.
%
% UNTITLED78('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED78.M with the given input arguments.
%
% UNTITLED78('Property','Value',...) creates a new UNTITLED78 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled78_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled78_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 untitled78
% Last Modified by GUIDE v2.5 26-May-2011 16:20:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @untitled78_OpeningFcn, ...
                   'gui_OutputFcn', @untitled78_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 untitled78 is made visible.
function untitled78_OpeningFcn(hObject, eventdata, handles, varargin)
handles.time=[10];
handles.resolution=[0.1];
% 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 untitled78 (see VARARGIN)
% Choose default command line output for untitled78
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled78 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled78_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)
sys=tf([1],[1 1]);
prompt = {'Enter input function as a function of time:'};
        dlg_title = 'Enter function';
        num_lines = 1;
        def = {'t'};
        answer = inputdlg(prompt,dlg_title,num_lines,def);
        if (~isempty(answer))
        if (~isempty(answer{1}))
        t = 0:handles.resolution:handles.time;
        v = str2func(['@(t) ' answer{1}])
        [y T]=lsim(sys,v(t),t);
        axes(handles.axes1);
        axis([0 handles.time 0 10])
        plot(t,y);
        guidata(hObject,handles);
        else
        warndlg('Invalid variable','!!Warning!!')
        end
        end
        guidata(hObject,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)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
prompt={'Time','resolution'};
defans={num2str(handles.time),num2str(handles.resolution)}; 
answer=inputdlg(prompt,'Limit Of Time & Resolution',1,defans,'on');
if (~isempty(answer))
    if (~isempty(answer{1}))&(~isempty(answer{2}))
    handles.time=str2num(answer{1});
    handles.resolution=str2num(answer{2});
    guidata(hObject,handles);
else
        warndlg('Invalid variable','!!Warning!!')
    end
end
guidata(hObject,handles);
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Hi there!! Here is my gui. I have an inputdialogbox and i use it to insert a user-defined function. The function can be in this form: '2*t+2' or 'sin(2*pi*0.1*t)' or '1' or 't^2'. For the first 2 forms everything is fine but for the last 2 (constant and polynomial functions) there is problem. I want to have one function which recognize EVERY function that I insert. How can I do that? Could someone help me? Thanks in advance!!
댓글 수: 0
채택된 답변
추가 답변 (1개)
  Matt Fig
      
      
 2011년 5월 27일
        The error from t^2 is because the user did not enter a vectorized function. The user should enter: t.^2 (notice the dot). However you can do this for the user with the VECTORIZE function:
eval(['v = @(t) ',vectorize(answer{1}),';'])
If you use the above to replace this line:
v = str2func(['@(t) ' answer{1}]);
then you will get the results you expect. I do not know what caused the error from entering a constant, as my version of STR2FUNC doesn't work with args that aren't function names.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Event Functions에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

