Index Out of Bounds Error - Troubleshooting in MATLAB

In summary, the conversation is about the speaker working on a piece of code to generate plots, specifically focusing on calculating the Mach number for different values of mdotf. The code also takes into account other variables such as Tt4_Tt2_ratio, pi_c, eff_c, and mdotcorr2. The speaker has encountered an indexing problem between MATLAB and Excel and has found a workaround by adding (RPMline) to the end of certain formulas. They are still unsure why MATLAB did not like the original coding method.
  • #1
roldy
237
2
I'm working on a piece of code to help me generate plots. Right now I'm focusing on the calculation stuff.

Here's what I need to do.
For each RPMline, calculate the Mach number (M0) for mdotf=.1,.2,.3,.4,.5,.6,.7
I read values of Tt4_Tt2_ratio, pi_c, eff_c, mdotcorr2 from the Excel sheet. As you can see in the code, the value of M0 not only depends on mdotf but also Tt4_Tt2_ratio, pi_c, eff_c, mdotcorr2.

So I have an array called mdotf of all the values of mdotf I need to use. This returns an array called M0, which contains all the Mach numbers based on the mdotf used. After doing this for the first RPMline, it should repeat for the second RPMline and keeps doing this until I've done this for all 8 RPMlines. What I tried to do was set up a loop for the RPMline that would increment Tt4_Tt2_ratio, pi_c, eff_c, mdotcorr2.

I'm always getting this message:
Attempted to access Tt4_Tt2_ratio(2); index out of bounds because numel(Tt4_Tt2_ratio)=1.

If, instead of using the loop, I put in values 1-8 in place of the variable RPMline I get a result. There's some kind of indexing problem between MATLAB and Excel but I can't figure out what it is.
 

Attachments

  • Performance Envelope.zip
    81.5 KB · Views: 193
Physics news on Phys.org
  • #2
Ok, I got this to work by getting rid of

Tt4_Tt2_ratio=Tt4_Tt2_ratio(RPMline);
pi_c=pi_c(RPMline);
eff_c=eff_c(RPMline);
mdotcorr2=mdotcorr2(RPMline);

And in the formulas just added (RPMline) to the end of Tt4_Tt2_ratio,pi_c, eff_c, mdotcorr2. It looks tacky but it works. I would still like to know why MATLAB did not like the way I had it coded.
 

1. What is an Index Out of Bounds Error in MATLAB?

An Index Out of Bounds Error in MATLAB occurs when the index used to access a specific element in an array or matrix is outside the range of valid indices for that variable. This means that the index is either too large or too small for the size of the variable.

2. What causes an Index Out of Bounds Error in MATLAB?

An Index Out of Bounds Error in MATLAB can be caused by a variety of reasons, such as using an incorrect index variable, not properly initializing a variable, or attempting to access an element in a higher dimension of a matrix than it has been defined for.

3. How can I troubleshoot an Index Out of Bounds Error in MATLAB?

To troubleshoot an Index Out of Bounds Error in MATLAB, you can start by checking the index variables used in the code to ensure they are within the range of valid indices for the variable. Additionally, you can use the built-in debugging tools in MATLAB to step through the code and identify where the error is occurring.

4. How can I prevent Index Out of Bounds Errors in MATLAB?

To prevent Index Out of Bounds Errors in MATLAB, it is important to properly initialize variables and double-check the index variables used in the code. You can also use conditional statements to check the validity of the index before attempting to access the element in an array or matrix.

5. Can an Index Out of Bounds Error in MATLAB be fixed?

Yes, an Index Out of Bounds Error in MATLAB can be fixed by identifying the source of the error and making the necessary corrections to the code. This may involve adjusting the index variables, redefining the size of the variable, or using conditional statements to check for valid indices.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
939
Back
Top