Help with 'Index out of bounds error'

1 view (last 30 days)
Here is my code so far
% Chris Corbett;
% Set 16;
% User input for the galaxy number, with result treated as string;
x = input('Please choose a number from 1 to 5 for the galaxy number: ','s');
% Reading in the galaxy data;
g = ['galaxy',x,'.txt'];
a = dlmread(g);
% Taking the wavelength from the data;
w = a(:,1);
% Taking the flux from the data;
f = a(:,2);
% Plotting the flux against the frequency;
plot (w,f);
xlabel ('Wavelength (nm)');
ylabel ('Spectral Flux (Arbitrary Units)');
title (['Graph for Galaxy ',x]);
% Importing the red shift data;
r = dlmread('redshifts.txt');
% Picking out associated red shift;
s = r(x,2);
% Importing the wavelength data;
y = dlmread('wavelengths.txt');
% Calculating the observed wavelength;
obsw = y.*(1+s);
plot(obsw,0.5,'kd')
It involves taking data from various files and then plotting various graphs with it. If more explanation is needed on this I can explain it. But basically
Galaxy# has 2 columns and a lot of rows (too many too count) Redshifts has 2 columns and 5 rows Wavelengths has 1 column and 8 rows
When I run the code I get the error
??? Attempted to access r(49,2); index out of bounds
because size(r)=[5,2].
I'm guessing this is perhaps something to do with the program thinking r is bigger than it is.
If I add a line just before the red shift data is imported that sets x to a value, such as x = 1; There is no problem.
I'm really not sure how to fix this, does anybody have any suggestions?
Thank you. Chris

Accepted Answer

Walter Roberson
Walter Roberson on 7 Mar 2012
You input x as a string (character), but then you try to use that character as the array index.
  1 Comment
Chris Corbett
Chris Corbett on 7 Mar 2012
Thanks for the quick reply mate, worked a treat.
It didn't even occur to me.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!