Error: Index exceeds matrix dimensions?

The data describes a smooth general curve that then juts up in the middle, and then comes back down and continues the first curve. This function attempts to find the area under the abnormal curve in the middle. Matlab says there is an error in this line: Area=Area+((((func(y)-specdata(y))+(func(z)-specdata(z)))/2)*(z-y));
Thanks in advance
function [ Area ] = Area( func, freq, specdata)
%UNTITLED2 Uses the input function as well as the input data to find the
%area under the spectral lines. This is done using many small trapezoids in
%between each consecutive set of points, finding the values according to the
%function and using the data. The area function is ((a+b)/2)*h.
Area=0;
j=1;
r=length(freq);
while j<r
y=freq(j);
z=freq(j+1);
Area=Area+((((func(y)-specdata(y))+(func(z)-specdata(z)))/2)*(z-y));
j=j+1;
end
end

 Réponse acceptée

Adam
Adam le 3 Déc 2014

2 votes

Before you do anything else and before I would even consider whether there are other problems in the code, you cannot name a variable the same as a function. In normal Matlab usage this is bad, but to name a variable the same as the function whose workspace that variable is in is surely catastrophic.
I've never tried doing it to know exactly what takes precedence where, but just make sure you never do it then you don't need to know exactly what happens!
There may well be other problems with that line too.
The simplest way to find those yourself is to break the line down into smaller components between each operator and then you can see what size each component is. This way you quickly see if your code is trying to add together two arrays of different dimension or se which part of the line is causing an indexing error.

5 commentaires

Mark
Mark le 3 Déc 2014
Oh, that was just stupid of me. It doesnt seem that just renaming the variable fixed it though. Im going to try to break the line down now. Thanks!
Mark
Mark le 3 Déc 2014
I got it figured out. By the way, i switched the name of the variable back to Area, the same as the function name, and it didn't mess up the function at all. Maybe its just lucky this time. Thanks again
Adam
Adam le 3 Déc 2014
It isn't obvious what your inputs represent, but is freq expected to be used as something to index into another vector?
For your code to work the maximum value present in your 'freq' input must be less than or equal to the length of your specdata input
Mark
Mark le 3 Déc 2014
Freq and specdata are two vectors with data that was given to us. Theyre exactly the same length, basically like a series of x and y. Like I said, I got it figured out by going into the line. I was incorrect in saying specdata(y), since that told the function to look for a "y value" for an"x value" that didnt exist. Should have been specdata(j)
Guillaume
Guillaume le 3 Déc 2014
And the morale of the story is: use names that represent the purpose of a variable rather than j, r, y, z.
You may now know what these stand for, but in a year's time when you reread your code, you'll have to spend some time understand what they do.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by