0

I have a matrix in the form of a text file, I was hoping to scan it using MATLAB, and scan for the maximum value in between 2 points (1.5 and 2) and use that as a threshold.

I wrote a code but it rerturned an error.

    [filename, pathname] = uigetfile('*txt', 'Pick text file');
data = dlmread(fullfile(pathname, filename)); 
t=data(:,1);
N = size(t,1);
m= max(data(1.5,2));
figure;
threshold = m;

Error in file (line 214) m= max(data(1.5,2));

Matlaber
  • 35
  • 7
  • 1
    You are currently asking it to find a value using 1.5 as an index, which isn't possible. I'd recommend reading the matlab help on logical operators. – etmuse Sep 25 '18 at 10:22
  • @etmuse I know the data range which is between 1.5 and 2 so was hoping to scan the data and find the maximum values and use it as threshold – Matlaber Sep 25 '18 at 10:25
  • 1
    As stated: you're using `1.5` as *index*, `data(A,B)` gives you the value of `data` on the indices as given by `A,B`. MATLAB requires your indices to be nonnegative, real integers. Use logical operators to define your range of `[1.5 2]`, then use `max` on the resulting set. – Adriaan Sep 25 '18 at 10:29

1 Answers1

1

data(1.5,2) does not ask for elements of data with values between 1.5 and 2; it asks for the element of data on the "1.5th" row and 2nd column, which is clearly undefined. Indices have to be integers.

The elements of data with values between 1.5 and 2 can be obtained with

data(data > 1.5 & data < 2)

so you can get the largest of these using

m = max(data(data > 1.5 & data < 2));
Will
  • 1,835
  • 10
  • 21