Need assistance with Fourier analysis and GUI

조회 수: 1 (최근 30일)
Christopher Carey
Christopher Carey 2018년 4월 14일
댓글: Walter Roberson 2018년 4월 14일
function varargout = gui212(varargin)
% GUI212 MATLAB code for gui212.fig
% GUI212, by itself, creates a new GUI212 or raises the existing
% singleton*.
%
% H = GUI212 returns the handle t0 a new GUI212 or the handle t0
% the existing singleton*.
%
% GUI212('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI212.M with the given input arguments.
%
% GUI212('Property','Value',...) creates a new GUI212 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied t0 the GUI before gui212_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed t0 gui212_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance t0 run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text t0 modify the response t0 help gui212
% Last Modified by GUIDE v2.5 13-Apr-2018 21:27:15
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui212_OpeningFcn, ...
'gui_OutputFcn', @gui212_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 gui212 is made visible.
function gui212_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle t0 figure
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments t0 gui212 (see VARARGIN)
% Choose default command line output for gui212
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui212 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned t0 the command line.
function varargout = gui212_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle t0 figure
% eventdata reserved - t0 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;
function T0_Callback(hObject, eventdata, handles)
% hObject handle t0 T0 (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of T0 as text
% str2double(get(hObject,'String')) returns contents of T0 as a double
% --- Executes during object creation, after setting all properties.
function T0_CreateFcn(hObject, eventdata, handles)
% hObject handle t0 T0 (see GCBO)
% eventdata reserved - t0 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 a0_Callback(hObject, eventdata, handles)
% hObject handle t0 a0 (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of a0 as text
% str2double(get(hObject,'String')) returns contents of a0 as a double
% --- Executes during object creation, after setting all properties.
function a0_CreateFcn(hObject, eventdata, handles)
% hObject handle t0 a0 (see GCBO)
% eventdata reserved - t0 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 an_Callback(hObject, eventdata, handles)
% hObject handle t0 an (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of an as text
% str2double(get(hObject,'String')) returns contents of an as a double
% --- Executes during object creation, after setting all properties.
function an_CreateFcn(hObject, eventdata, handles)
% hObject handle t0 an (see GCBO)
% eventdata reserved - t0 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 bn_Callback(hObject, eventdata, handles)
% hObject handle t0 bn (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of bn as text
% str2double(get(hObject,'String')) returns contents of bn as a double
% --- Executes during object creation, after setting all properties.
function bn_CreateFcn(hObject, eventdata, handles)
% hObject handle t0 bn (see GCBO)
% eventdata reserved - t0 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 n_Callback(hObject, eventdata, handles)
% hObject handle t0 n (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of n as text
% str2double(get(hObject,'String')) returns contents of n as a double
% --- Executes during object creation, after setting all properties.
function n_CreateFcn(hObject, eventdata, handles)
% hObject handle t0 n (see GCBO)
% eventdata reserved - t0 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 x_Callback(hObject, eventdata, handles)
% hObject handle t0 x (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of x as text
% str2double(get(hObject,'String')) returns contents of x as a double
% --- Executes during object creation, after setting all properties.
function x_CreateFcn(hObject, eventdata, handles)
% hObject handle t0 x (see GCBO)
% eventdata reserved - t0 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
% --- Executes on button press in Run.
function Run_Callback(hObject, eventdata, handles)
% hObject handle t0 Run (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
T0 = str2double(get(handles.T0, 'String'));
a0 = str2double(get(handles.a0, 'String'));
bn = str2double(get(handles.bn, 'String'));
an = str2double(get(handles.an, 'String'));
max_N = str2double(get(handles.n, 'String'));
xrange = str2double(get(handles.x, 'String'));
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xrange,max_N)
plot(x,f)
function [x,f]=my_Fourier_series_generator(T0,a0,an,bn,xrange,max_N)
x=xrange;
% frequency wn
w=2*pi/T0;
% initializing f at a0
f=a0;
syms n;
% Fourier series implementation
for n=1:max_N
an2=eval(an);
bn2=eval(bn);
f=f+an2*cos(w*n*x)+bn2*sin(w*n*x);
end
These are my errors
Error using eval
Must be a string scalar or character vector.
Error in gui212>my_Fourier_series_generator (line 244)
an2=eval(an);
Error in gui212>Run_Callback (line 226)
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xrange,max_N)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gui212 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gui212('Run_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
These are the arguments i entered
t0=2 a0=0.504 an=0.504*(2/(1+(16*(n^2)))) bn=0.504*((8*n)/(1+(16*(n^2)))) xrange=0:0.1:10 n=500

답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 14일
You have
an = str2double(get(handles.an, 'String'));
so we know that an must be a double (and would be NaN if the an edit field did not contain a valid scalar.)
You call
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xrange,max_N)
so you are passing that scalar (possibly nan) value as the third parameter to the function.
Inside the function you have
an2=eval(an);
so you are trying to eval() that scalar double. It is not permitted to eval() a scalar double: you can only eval() a string object or a character vector.
  댓글 수: 9
Christopher Carey
Christopher Carey 2018년 4월 14일
편집: Walter Roberson 2018년 4월 14일
however im getting an error when trying a different input these are the inputs
T0=1 a0=1/2 bn=0 an=2/(n*pi)*sin(n*pi/2) bn=0 n=100 xmin=-10 xmax=10 xincrement=0.01
this is my code now
function Run_Callback(hObject, eventdata, handles)
% hObject handle t0 Run (see GCBO)
% eventdata reserved - t0 be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
T0 = str2double(get(handles.T0, 'String'));
a0 = str2double(get(handles.a0, 'String'));
bn = (get(handles.bn, 'String'));
an = (get(handles.an, 'String'));
max_N = str2double(get(handles.n, 'String'));
xmin = str2double(get(handles.x1, 'String'));
xmax = str2double(get(handles.x2, 'String'));
xincrement = str2double(get(handles.x3, 'String'));
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xmin,xmax,xincrement,max
_N)
plot(x,f)
function
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xmin,xmax,xincrement,max_N)
x1=xmin;
x2=xmax;
x3=xincrement;
x=xmin:xincrement:xmax;
% frequency wn
w=2*pi/T0;
% initializing f at a0
f=a0;
syms n;
% Fourier series implementation
for n=1:max_N
an2=eval(an);
bn2=eval(bn);
f=f+an2*cos(w*n*x)+bn2*sin(w*n*x);
end
This is my error now
Error using gui212>my_Fourier_series_generator (line 248)
Error: This statement is incomplete.
Error in gui212>Run_Callback (line 228)
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xmin,xmax,xincrement,max_N)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gui212 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gui2
12('Run_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Walter Roberson
Walter Roberson 2018년 4월 14일
We do not know which line is line 248
Reminder:
>> str2double('1/2')
ans =
NaN
1/2 is not a scalar double, it is an expression and cannot be handled by str2double()

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by