Index exceeds matrix dimensions error

2 views (last 30 days)
Alexandra
Alexandra on 21 Nov 2014
Commented: Adam on 21 Nov 2014
I have created a simple GUI and part of my code comes from this website (<http://homepages.udayton.edu/~hardierc/ece203/sound.htm>) but when I try to run the high pass or low pass filter or to minimize the vocals, it gives me the
Index exceeds matrix dimensions error.
I want to do this using the handel music that MATLAB already has built in and I got most of everything else to work. I just don't get why the high and low pass filters are not working. Here is my code:
function varargout = EE384_FinalProject_Ams(varargin)
% EE384_FINALPROJECT_AMS MATLAB code for EE384_FinalProject_Ams.fig
% EE384_FINALPROJECT_AMS, by itself, creates a new EE384_FINALPROJECT_AMS or raises the existing
% singleton*.
%
% H = EE384_FINALPROJECT_AMS returns the handle to a new EE384_FINALPROJECT_AMS or the handle to
% the existing singleton*.
%
% EE384_FINALPROJECT_AMS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EE384_FINALPROJECT_AMS.M with the given input arguments.
%
% EE384_FINALPROJECT_AMS('Property','Value',...) creates a new EE384_FINALPROJECT_AMS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before EE384_FinalProject_Ams_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to EE384_FinalProject_Ams_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 EE384_FinalProject_Ams
% Last Modified by GUIDE v2.5 21-Nov-2014 08:17:43
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @EE384_FinalProject_Ams_OpeningFcn, ...
'gui_OutputFcn', @EE384_FinalProject_Ams_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 EE384_FinalProject_Ams is made visible.
function EE384_FinalProject_Ams_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 EE384_FinalProject_Ams (see VARARGIN)
% Choose default command line output for EE384_FinalProject_Ams
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes EE384_FinalProject_Ams wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = EE384_FinalProject_Ams_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)
% 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)
reverseplay;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
digidelay;
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, 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)
digidelay2;
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
digidelay3;
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
highpassfilter;
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
lowpassfilter;
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
speedup;
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
slowdown;
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
minimizevocals;
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
exitprogram;
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
playoriginalsong;
*Here is the high pass filter code:*
function highpassfilter
load handel;
out=y;
for n=2:length(y)
out(n,1)=y(n,1)-y(n-1,1); % left
out(n,2)=y(n,2)-y(n-1,2); % right
end
soundsc(out,Fs) % high pass filtered
end
*Here is the low pass filter code:*
function lowpassfilter
load handel;
out=y;
for n=2:length(y)
out(n,1)=.9*out(n-1,1)+y(n,1); % left
out(n,2)=.9*out(n-1,2)+y(n,2); % right
end
soundsc(out,Fs)
end
*Here is the minimize the vocals code:*
function minimizevocals
load handel;
soundsc(y(:,1)-y(:,2),Fs); % reduced vocal
end
  2 Comments
Adam
Adam on 21 Nov 2014
It is always extremely helpful if you can remove any code that is clearly not relevant to the problem when you paste code in. It is true that often someone will remove too much code thinking it isn't relevant when it is, but clearly all those pushbutton callbacks with one-line commands are not all relevant - they just make it harder to see the code that is problematic.

Sign in to comment.

Answers (2)

Adam
Adam on 21 Nov 2014
Edited: Adam on 21 Nov 2014
A snippet from your code:
out=y;
for n=2:length(y)
out(n,1)=y(n,1)-y(n-1,1); % left
out(n,2)=y(n,2)-y(n-1,2); % right
end
y is a single signal (vector) here with 2nd dimension 1 so trying to access out(n, 2) is not going to work.
If you are rewriting over everything in 'out' anyway then initialise it as:
out = zeros( numel(y), 2 );
Even then though the fact you access y(n,2) is simply not going to work for the same reason and I really don't know what your intention is there.
Assuming 'out' really should be the same size as 'y' then you can declare 'out' as:
out = zeros( size(y) );
rather than what I put above, but you still need to do something about the size of y in that case as 73113x1

Alexandra
Alexandra on 21 Nov 2014
I am not really sure of the intention of the code is. I got the code from the website mentioned in the first sentence. Thank you for the answer! Hmm... it still doesn't like it and it gives me this in the command window when I click the high pass filter buttion:
Index exceeds matrix dimensions.
Error in highpassfilter (line 8) out(n,2)=y(n,2)-y(n-1,2); % right
Error in EE384_FinalProject_Ams>pushbutton5_Callback (line 113) highpassfilter;
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in EE384_FinalProject_Ams (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)EE384_FinalProject_Ams('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
  1 Comment
Adam
Adam on 21 Nov 2014
It will still give that if y is still the same as it was, it has no 2nd dimension (or rather, the size of the 2nd dimension is 1 since all matrices in Matlab are at least 2-dimensional) which is why the index exceeds the matrix dimensions

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!