Trying to run a photometry program but I keep getting an error I don't quite understand

I'm trying to run this photometry program I made, but I keep running into problems.
The program itself is basically done, I just need to make it able to run now.
%Pcitures
files=dir("C:\Users\Jeff\Documents\MATLAB\Cepheider - fits (1)")
%Offset
offset=importdata('offsets.txt');
offx=offset(:,1);
offy=offset(:,2);
%Orginale pic is image1 'o4201193.10.fts.fts'
hold on;
imshow(image1, [100 650])
%Time data
Time = dlmread('tid.txt');
Time_real = (Time-2455922.728);
%Clip S2
UdklipS2 = image1(142:155,188:202);
%Clip V1
UdklipV1 = image1(233:245,208:222);
%Star S2
startyS2=142;%Top
endyS2=155;%Bot
startxS2=188;%Left
endxS2=202;%Right
Radius_S2 = 7.5;
%Cepheid V1
startyV1=233;%Top
endyV1=248;%Bot
startxV1=208;%Left
endxV1=222;%Right
%Making arrays
Fluxx_V1 =[];%Counts V1
Fluxx_S2 =[];%Counts S2
UdklipV1 =[];
UdklipS2 =[];
MagnitudeV1 =[];
MagnitudeS2 =[];
Times =[];
for i=1:98
images=fitsread(files(i+2).name);
UdklipV1 = images(startyV1+offx(i):endyV1+offx(i),startxV1+offy(i):endxV1+offy(i));
BagrundV1 = images(startyV1+offx(i):endyV1+offx(i),startxV1+offy(i)+14:endxV1+offy(i)+14);%+14 forkydning
Times = [Times, Time_real];
%Photometry
[xV1, yV1]=size(UdklipV1);
ImageSizexV1= xV1; ImageSizeyV1= yV1; RadiusV1= 7;%Dimensions
[xsV1, ysV1]= meshgrid(1:ImageSizexV1, 1:ImageSizeyV1);
circleV1= (xsV1-(endxV1-startxV1)/2).^2+(ysV1-(endyV1-startyV1)/2).^2;%Circle
V1=circleV1*UdklipV1;
Flux_af_V1 = sum(sum(V1));
Bagrund_af_V1 = circleV1'.*BagrundV1;
Flux_bagrund_V1 = sum(sum(Bagrund_af_V1));
Real_fluxV1 = Flux_af_V1-Flux_bagrund_V1;
%Gathering data
Fluxx_V1=[Fluxx_V1, Real_fluxV1];
%Magnitude
MagnitudeV1=[MagnitudeV1, -2.5*log10(Real_fluxV1)];
%Star S2
UdklipS2= images(startyS2+offx(i):endyS2+offx(i),startxS2+offy(i):endxS2+offy(i));
BagrundS2=images(startyS2+offx(i)+13:endyS2+offx(i)+13);startxV1+offy(i):endxV1+offy(i);%
%Photometry
[xS2, yS2]=size(UdklipS2);
ImageSizexS2=xS2; ImageSizeyS2=yS2; RadiusS2 = 7.5;%Dimensions
[xsS2, ysS2]=meshgrid(1:ImageSizexS2, 1:ImageSizeyS2);
circleS2 = (xsS2-(endxS2-startxS2)/2).^2+(ysS2-(endyS2-startyS2)/2).^2;%Circle
S2=circleS2*UdklipS2;
Flux_af_S2 = sum(sum(S2));
Bagrund_af_S2 = circleS2.*BagrundS2;
Flux_bagrundS2 = sum(sum(Bagrund_af_S2));
Real_fluxS2 = Flux_af_S2-Flux_bagrundS2;
%Gathering data
Fluxx_S2=[Fluxx_S2, Real_fluxS2];
%Magnitude
MagnitudeS2=[MagnitudeS2, -2.5*log10(Real_fluxS2)];
end
Magnitude_relationship = (MagnitudeV1-MagnitudeS2);
p = 3.784
fase= mod(Time_real,p)./p;
fig8 = figure(8);
hold on;
plot(fase, Magnitude_relationship, '.');
title('Faseplot')
xlabel('Periode')
ylabel('Delta(M)')
fig9 = figure(9);
hold on;
plot(Time, Magnitude_relationship)
title('Distance modulus asf func. of time')
xlabel('Time')
ylabel('Delta(M)')
But I get the error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Fotometri (line 252)
UdklipS2= images(startyS2+offx(i):endyS2+offx(i),startxS2+offy(i):endxS2+offy(i));

6 Comments

Did you use the debugger to stop execution at that line? That will allow you to calculate the indices. Are you sure the code is doing what you think it is doing?
If I run up until that point, it tells me that Index in position 1 exceeds array bounds.
But the first 2 data files, I can't use so i'm skipping them with my i+2, I believe the problem is that the offsets start at 1, whereas the data starts at 3
@Jeff Sejr you forgot to attach 'tid.txt'. Also, if the problem doesn't show up until the third data file, you're going to have to give us at least three files.
Well what I mean to say is that when I open the fits files with the dir, the first two fields in the workspace when I open it up are just '.', and not anything that's actually in my zipfile, also uploaded tid "time"

Sign in to comment.

Answers (1)

offx(14) is -197. startyS2 is 142. So startyS2+offx(i) is -55 when i is 14. So that line is trying to index images starting at row -55, which is not a valid index, hence the error. I would check that your offsets are correct.

9 Comments

I mean even if I change the high ones to be less than 35, it still doesn't work.
And I was given this data to use, maybe i'm doing something wrong with my code
Can you attach the adjusted offsets you tried (i.e., the ones with the high ones set to be less than 35)?
Does it still give the same error on the same line? Or some other error when using the modified offsets file?
Same error yes:
Index in position 1 exceeds array bounds.
Error in Fotometri (line 231)
UdklipV1 = images(startyV1+offx(i):endyV1+offx(i),startxV1+offy(i):endxV1+offy(i));
Thank you. That's actually a different error (previously was "Index in position 1 is invalid. Array indices must be positive integers or logical values.") on a different line (previously was on line 252).
This new error tells me that endyV1+offx(i) is greater than the size of image i in dimension 1. So it's a different error, but it's still caused by apparently incorrect indices.
I guess I would double-check two things:
1) make sure the ith image as returned by dir() corresponds to the ith row of offsets (actually the (i-2)th row of offsets since you skip the '.' and '..'), but the point is that dir() may not give the images in the same order the offsets are in.
2) make sure your expressions for indexing the images are correct. Specifically, you are adding startyV1 to offx(i), which, judging by the names of the variables, is one number relating to the y-direction and one relating to the x-direction. I mean, variable names can be anything, so this is not necessarily a problem; I would just check that these didn't get swapped around somewhere unintentionally.
Thank you for the tips, I do believe I definitely switched up the x and y offset, however, when I change the two I still get the error: Index in position 1 exceeds array bounds.
As for the dir, I start at the (i+2)th row which would be row 3, in the dir.
The files opened in the dir, are the same order as the data from the zip file, which are in the same order as the offsets.
I thought I had figured it out by moving the position of the +14 and+13 in the lines below like:
BagrundV1 = images(startyV1+offy(i):endyV1+offy(i),startxV1+14+offx(i):endxV1+14+offx(i))
but gave me the same error

Sign in to comment.

Products

Release

R2021b

Asked:

on 11 Dec 2021

Commented:

on 11 Dec 2021

Community Treasure Hunt

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

Start Hunting!