errors in spring mass gui code

1 次查看(过去 30 天)
Hello,
I am creating a spring mass GUI for my final project in MATLAB. It runs most of the time, but sometimes it gives me an error.
When I input:
position at time=0: 2
velocity at time=0: 1
mass: 2
Damping coefficient: 500
Spring constant: 16000
External Force: 3400
I get an error stating:
Array indices must be positive integers or logical values.
Error in SpringMassP>pushbutton1_Callback (line 220)
P=circular(y(k)+1,.5,.5,'r')
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in SpringMassP (line 44)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)SpringMassP('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
I am wondering if someone can tell me why? It seems like when I keep my damping coefficient under 200 i dont get errors. Heres the code and .fig attached.
function varargout = SpringMassP(varargin)
% SPRINGMASSP MATLAB code for SpringMassP.fig
% SPRINGMASSP, by itself, creates a new SPRINGMASSP or raises the existing
% singleton*.
%
% H = SPRINGMASSP returns the handle to a new SPRINGMASSP or the handle to
% the existing singleton*.
%
% SPRINGMASSP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SPRINGMASSP.M with the given input arguments.
%
% SPRINGMASSP('Property','Value',...) creates a new SPRINGMASSP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before SpringMassP_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to SpringMassP_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 SpringMassP
% Last Modified by GUIDE v2.5 19-May-2019 07:20:20
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SpringMassP_OpeningFcn, ...
'gui_OutputFcn', @SpringMassP_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 SpringMassP is made visible.
function SpringMassP_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 SpringMassP (see VARARGIN)
% Choose default command line output for SpringMassP
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes SpringMassP wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = SpringMassP_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;
function mass_Callback(hObject, eventdata, handles)
% hObject handle to mass (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 mass as text
% str2double(get(hObject,'String')) returns contents of mass as a double
% --- Executes during object creation, after setting all properties.
function mass_CreateFcn(hObject, eventdata, handles)
% hObject handle to mass (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 sprconst_Callback(hObject, eventdata, handles)
% hObject handle to sprconst (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 sprconst as text
% str2double(get(hObject,'String')) returns contents of sprconst as a double
% --- Executes during object creation, after setting all properties.
function sprconst_CreateFcn(hObject, eventdata, handles)
% hObject handle to sprconst (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 damp_Callback(hObject, eventdata, handles)
% hObject handle to damp (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 damp as text
% str2double(get(hObject,'String')) returns contents of damp as a double
% --- Executes during object creation, after setting all properties.
function damp_CreateFcn(hObject, eventdata, handles)
% hObject handle to damp (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 fext_Callback(hObject, eventdata, handles)
% hObject handle to fext (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 fext as text
% str2double(get(hObject,'String')) returns contents of fext as a double
% --- Executes during object creation, after setting all properties.
function fext_CreateFcn(hObject, eventdata, handles)
% hObject handle to fext (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
% --- 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)
%Below section deals with all the inputs from the user, graphs the
%results, and displays the equation.
syms x m u k f x(t)
f=str2sym(get(handles.fext,'String'))%Converting the input vars to symbolic
m=str2sym(get(handles.mass,'String'))
u=str2sym(get(handles.damp,'String'))
k=str2sym(get(handles.sprconst,'String'))
Dx=diff(x) %Defines Dx to be the first derivative of x
D2x=diff(x,2) %Defines D2x to be the second derivative of x
ode=m*D2x+u*Dx+k*x==f %Defining my differential spring mass equation to be "ode"
ICx=str2num(get(handles.initialposition,'String')) %Allowing user to enter initial conditions
ICv=str2num(get(handles.initialvelocity,'String'))
a=dsolve(ode,x(0)==ICx, Dx(0)==ICv)
set(handles.answer,'String',char(a));
axes(handles.axes2)
[time,position]=fplot(a,[0 5]);
%I want the below loop to run while the other graph is being drawn on
%handles.animation. Not quite sure how to do this..
%Somehow I need to get the below loop to plot what it is trying to plot in
%tandem with the plot for handles.animation starting on line 214
%for i=1:length(time)
% plot(time(i),position(i),'x')
% hold on
% plot(time(1:k),position(1:k))
%pause(.001)
%end
plot(time,position)
fplot(a,[0 5]); %one output will be the x axis and one will be the y. once we specify the function it will
Q=time; %This is the time array of the function graphed
V=position %This is the position array of the function graphed
%G=V(end);
%x0=sum(logical(diff(sign(V)))); %This obtains the number of times the
%graph crosses the origin, but i dont need that
axes(handles.Animation)
%A1=V(1); %I dont need these two
%B1=length(V);
y=V; %V is the position array of the graph that is graphed in axes2
for k=V(1):5:length(V)-1 %My loop is going from the first entry of V array, to the last entry,
hold on
A3=mean(V);
J=round(A3)
P=circular(y(k)+1,.5,.5,'r')
xleft=min(V);
% xright=max(V);
% axis([xleft xright -2 4])
X=[xleft-1,xleft+1,xleft+1+((V(k)-xleft)/9),xleft+1+2*((V(k)-xleft)/9),xleft+1+3*((V(k)-xleft)/9),xleft+1+4*((V(k)-xleft)/9),xleft+1+5*((V(k)-xleft)/9),1+y(k)]
Y=[.5,.5,1,0,1,0,1,.5]
plot(Y,X,'b','linewidth',1) %I changed these to plot y,x so we can have a vertical graph
set(gca,'Ydir','reverse')
pause(.2)
P=circular(y(k)+1,.5,.5,'w-')
plot(Y,X,'w','linewidth',1)
%set(gca,'Ydir','reverse')
end
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function answer_CreateFcn(hObject, eventdata, handles)
% hObject handle to answer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
checkboxValue = get(handles.checkbox1, 'value')
if checkboxValue
set(handles.text19, 'Visible', 'on');
else
set(handles.text19, 'Visible', 'off');
end
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 initialposition_Callback(hObject, eventdata, handles)
% hObject handle to initialposition (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 initialposition as text
% str2double(get(hObject,'String')) returns contents of initialposition as a double
% --- Executes during object creation, after setting all properties.
function initialposition_CreateFcn(hObject, eventdata, handles)
% hObject handle to initialposition (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 initialvelocity_Callback(hObject, eventdata, handles)
% hObject handle to initialvelocity (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 initialvelocity as text
% str2double(get(hObject,'String')) returns contents of initialvelocity as a double
% --- Executes during object creation, after setting all properties.
function initialvelocity_CreateFcn(hObject, eventdata, handles)
% hObject handle to initialvelocity (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
% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
a1=get(handles.popupmenu4,'Value'); %a=1-->overdamped, a=2-->underdamped,a=3-->critically damped
if a1==2
syms x(t)
Dx=diff(x)
D2x=diff(x,2)
ode=1*D2x+1*Dx+3*x==0
xSol1 = dsolve(ode,x(0)==1, Dx(0)==0)
axes(handles.axes7)
[time,position]=fplot(xSol1);
plot(time,position)
axis([-5 5 -8 6.8])
title('Underdamped')
end
if a1==3
syms x(t)
Dx=diff(x)
D2x=diff(x,2)
ode=1*D2x+4*Dx+3*x==0
xSol1 = dsolve(ode,x(0)==1, Dx(0)==0)
axes(handles.axes7)
[time,position]=fplot(xSol1);
plot(time,position)
axis([0 5 0 1.5])
title('Overdamped')
end
if a1==4
syms x(t)
Dx=diff(x)
D2x=diff(x,2)
ode=1*D2x+4*Dx+4*x==0
xSol1 = dsolve(ode,x(0)==1, Dx(0)==0)
axes(handles.axes7)
[time,position]=fplot(xSol1);
plot(time,position)
axis([-2 7 -2 1.1])
title('Critical')
end
% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 Animation_CreateFcn(varargin)
% --- 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)

采纳的回答

Geoff Hayes
Geoff Hayes 2019-5-19
William - the code around the error is
y=V; %V is the position array of the graph that is graphed in axes2
for k=V(1):5:length(V)-1 %My loop is going from the first entry of V array, to the last entry,
hold on
A3=mean(V);
J=round(A3)
P=circular(y(k)+1,.5,.5,'r')
% etc.
As your comment states, V is the position array. The code then iterates from V(1) to the one less than the length of V with a step size of 5. This may be fine for some cases, but what happens when V(1) is not an integer. Then k is not an integer, and you are using a non-integer index into y at
P=circular(y(k)+1,.5,.5,'r')
So do you really want to initialize k to be V(1) to be used as an index, or would you rather just have
for k=1:5:length(V)-1 %
and so be guaranteed an integer on each iteration of the loop?
  1 个评论
William Fantin
William Fantin 2019-5-19
You sir,
Are AMAZING!!! thank you so much, completely understood the answer!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by