Problem with error "Subscript indices must either be real positive integers or logicals"

Hello,
I know this is a common problem, but I don't know how to fix it in my own context. I get the following error on line 20 (Y = y(Ty)): "Subscript indices must either be real positive integers or logicals". I don't think I'm using an in-built function, and I checked Ty and y to see if there are any 0s, but there are none. I find it really strange as the code for "y" is the same as "x", but I'm only getting this error with "y". I am using Matlab R2017a.
My code:
clear all
close all
x = rand(1,38400);
fs = 128;
t = (0:length(x)-1)/fs;
ST = 10*fs;
EN = 60*fs;
T = t(ST:EN);
Tx = T*fs;
X = x(Tx);
y = rand(1,75000);
fs1 = 250;
t1 = (0:length(y)-1)/fs1;
ST1 = 10*fs1;
EN1 = 60*fs1;
T1 = t1(ST1:EN1);
Ty = T1*fs1;
Y = y(Ty); %Line 20
newyfs = fs1/fs;
ny = Y(1:newyfs:end);
nt = T1(1:newyfs:end);
nfs1 = fs1/newyfs;
[acor,lag] = xcorr(ny,X,'coef');
[~,I] = max(abs(acor));
timeDiff = lag(I);
figure
subplot(311); plot(Tx,X); title('X');
subplot(312); plot(nt,ny); title('Y');
subplot(313); plot(lag/nfs1,acor);
title('Cross-correlation')
I am practising this code so I can apply it to my data, where I want to check the correlation of two signals from the same source but each signal is recorded from different systems.

 Accepted Answer

Use
Y = y( round( Ty ) );
to ensure they are actually integers. at least one of them must have decimal values, however small.

3 Comments

Thank you very much! This helped. I didn't think there would be a decimal in there but when I used isinteger, it came up false.
isinteger tests whether the data is of integer type, not whether the variable is integer valued. Anything that is the result of maths involving operations more complicated than addition and subtraction may result in data that is not quite integer valued though, even if you start with integers and the exact analytic result should be an integer - it's just the way floating point maths works on a computer.
Oh. I understand. I'll need to keep that in mind. Thank you.

Sign in to comment.

More Answers (0)

Products

Asked:

on 1 May 2018

Commented:

on 3 May 2018

Community Treasure Hunt

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

Start Hunting!