Trouble in code with error "index exceeds number of elements"

4 views (last 30 days)
Hello All!
I am creating a program that can evaulate spring mass problems in matlab. In my program the user is able to interact with a GUI by adding the mass of the block, the spring constant, any damping that may be present, and an external force. My program outputs the result and shows a graph of what it will look like. In addition to this I am also trying to create a sort of "animation" in two parts. The first part of the animation is to draw the "mass" portion of the spring mass system, I have achieved this by writing a for loop that draws a rectangular box in red, then draws over that location in white (basically erasing it), then drawing another rectangular box a little bit to the right of that, so it looks like the box is moving. THis is the issue I am having with my code, for some reason MATLAB is giving me an error within my for loop saying "index exceeds the number of elements". My teacher helped me create this for loop so I am not too sure what this error means. I am new to MATLAB so any help would be appreciated.
I will attach my code below along with a .fig file of my GUI
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 10-May-2019 08:08:35
% 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)
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)
D2x=diff(x,2)
ode=m*D2x+u*Dx+k*x==f
a=dsolve(ode,x(0)==1, Dx(0)==1)
set(handles.answer,'String',char(a));
x=0:0.1:10;
axes(handles.axes2)
fplot(eval(a))
axes(handles.Animation)
y=1:20;
for k=1:length(y)
hold on
plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,0,2,2,0,],'r-')
axis([0 30 -5 5])
pause(.2)
plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,0,2,2,0,],'w-')
end
y=20:-1:1;
for k=1:length(y)
hold on
axis([0 30 -5 5])
plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,0,2,2,0,],'r-')
pause(.2)
plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,0,2,2,0,],'w-')
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

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 12 May 2019
Hi,
In your code, k=1:length(y) has to be k=1:length(y)-1. Because you are trying to access y(k+1) when you have the length of y up to k.
Good luck.
  1 Comment
William Fantin
William Fantin on 12 May 2019
Thank you! I appreciate it. I may have another questions once I digest exactly what that means, but it made my code work so THANK YOU!!!!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!