How to accept non integer number in a trapezoidal rule formula
1 次查看(过去 30 天)
显示 更早的评论
Hello, I'm stuck for doing a trapezoidal rule GUI with error of:
??? Error using ==> colon
Double operands interacting with char operands must have integer values.
Error in ==> Trapezoidal_rule>value_n_Callback at 157
handles.x= a:h:b;
here is the full code;
function varargout = Trapezoidal_rule(varargin)
% TRAPEZOIDAL_RULE MATLAB code for Trapezoidal_rule.fig
% TRAPEZOIDAL_RULE, by itself, creates a new TRAPEZOIDAL_RULE or raises the existing
% singleton*.
%
% H = TRAPEZOIDAL_RULE returns the handle to a new TRAPEZOIDAL_RULE or the handle to
% the existing singleton*.
%
% TRAPEZOIDAL_RULE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TRAPEZOIDAL_RULE.M with the given input arguments.
%
% TRAPEZOIDAL_RULE('Property','Value',...) creates a new TRAPEZOIDAL_RULE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Trapezoidal_rule_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Trapezoidal_rule_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 Trapezoidal_rule
% Last Modified by GUIDE v2.5 09-Apr-2013 23:26:20
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Trapezoidal_rule_OpeningFcn, ...
'gui_OutputFcn', @Trapezoidal_rule_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 Trapezoidal_rule is made visible.
function Trapezoidal_rule_OpeningFcn(hObject, ~, 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 Trapezoidal_rule (see VARARGIN)
% Choose default command line output for Trapezoidal_rule
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Trapezoidal_rule wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Trapezoidal_rule_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 calculate.
function calculate_Callback(hObject, eventdata, handles)
% hObject handle to calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
y = handles.equation;
handles.w = trapz(x,y);
calculate = handles.w;
guidata (hObject, handles);
% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
% hObject handle to reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function value_a_Callback(hObject, eventdata, handles)
% hObject handle to value_a (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 value_a as text
% str2double(get(hObject,'String')) returns contents of value_a as a double
handles.value_a = get (hObject,'string');
guidata (hObject, handles)
% --- Executes during object creation, after setting all properties.
function value_a_CreateFcn(hObject, eventdata, handles)
% hObject handle to value_a (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 value_b_Callback(hObject, eventdata, handles)
% hObject handle to value_b (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 value_b as text
% str2double(get(hObject,'String')) returns contents of value_b as a double
handles.value_b = get (hObject, 'string');
guidata (hObject, handles)
% --- Executes during object creation, after setting all properties.
function value_b_CreateFcn(hObject, eventdata, handles)
% hObject handle to value_b (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 value_n_Callback(hObject, eventdata, handles)
% hObject handle to value_n (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 value_n as text
% str2double(get(hObject,'String')) returns contents of value_n as a double
handles.value_n = str2double (get(hObject, 'string'));
n = handles.value_n;
a = handles.value_a;
b = handles.value_b;
handles.h =(b-a)/n;
h = handles.h;
handles.x= a:h:b;
x=func(handles.x)
['The value of a,b,n and h are: a= ',num2str(a),', b= ',num2str(b),', n= ',num2str(n),', h= ',num2str(h)]
guidata (hObject, handles);
% --- Executes during object creation, after setting all properties.
function value_n_CreateFcn(hObject, eventdata, handles)
% hObject handle to value_n (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 equation_Callback(hObject, eventdata, handles)
% hObject handle to equation (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 equation as text
% str2double(get(hObject,'String')) returns contents of equation as a double
handles.equation = str2double(get(hObject, 'string'));
guidata (hObject, handles);
% --- Executes during object creation, after setting all properties.
function equation_CreateFcn(hObject, eventdata, handles)
% hObject handle to equation (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 answer_1_Callback(hObject, eventdata, handles)
% hObject handle to answer_1 (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 answer_1 as text
% str2double(get(hObject,'String')) returns contents of answer_1 as a double
answer_1 = get(calculate)
% --- Executes during object creation, after setting all properties.
function answer_1_CreateFcn(hObject, eventdata, handles)
% hObject handle to answer_1 (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
0 个评论
采纳的回答
Jan
2013-4-18
For this line:
handles.x= a:h:b;
you obtain the values of a and b by:
handles.value_a = get (hObject,'string')
handles.value_b = get (hObject,'string')
Then they are strings. For the value n you successfully apply str2double already:
handles.value_n = str2double (get(hObject, 'string'));
This is required for a and b also.
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!