How do I fix a improper index matrix reference error

2 views (last 30 days)
Here is what is in my code:
function precip(data)
%find the data for variable right before a rain event
%create a scatterplot to correlate sets of data
%create a counting vector for points along the length of the data set
index=1:length(data.T1);
previous=index-1;
previous(1)=1;
%create variable for the change in the data set
data.deltaT1=data.T1(index)-data.T1(previous);
%find the rain events in the year-long set of data
rain= data.PPN>0;
before=data.numtime(rain)-2;
%scatter plot of the useful correlations between data sets
plot(data.PPN(rain).data.T1(rain));
end

Answers (2)

Sven
Sven on 2 Dec 2011
Here's how to debug such a problem: Type into the command line:
dbstop if error
Then, run your program. MATLAB will stop at the specific line that makes the error. There will be a little green arrow next to that line, and you'll see what the problem is. At that point, you can check the workspace (or just type the variable names you want to check into the command window) to find the problem. My guess (since we don't have your "data", so we don't know which line caused the error you're talking about) is that length(data.T1) is zero, or your data.PPN, data.numtime, data.T1 variables have different sizes.

Matt Tearle
Matt Tearle on 2 Dec 2011
A few things that will help us diagnose the problem:
  1. Format your code
  2. Show the exact error message so that we know on what line it occurs
  3. Give us the structure of the data variable you're passing in as an input. It apparently is a structure with fields T1, numtime and PPN. What are the dimensions of those fields? And what size is data (if it is a structure array, that could cause some funky effects).
Some other things I notice.
  • The line data.deltaT1=data.T1(index)-data.T1(previous); creates a new field to the data structure. However, data is not returned as an output, so any changes made to it will be lost.
  • Similarly, before doesn't seem to be used.
  • The first four lines of code can all be replaced with data.deltaT1 = [0,diff(data.T1)]; (or [0;diff...] if data.T1 is a column vector).

Community Treasure Hunt

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

Start Hunting!