Clear Filters
Clear Filters

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

2 views (last 30 days)
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

Adam
Adam on 1 May 2018
Use
Y = y( round( Ty ) );
to ensure they are actually integers. at least one of them must have decimal values, however small.
  3 Comments
Adam
Adam on 2 May 2018
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.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!