How to resolve an error for undefined function or variable 'x' within a function?

5 views (last 30 days)
I am trying to run code that calls in the function AMI from file (AMI.m) and it keeps giving me an error:
"undefined function or variable 'x' for line 46 A=sparse(x);
function [tau,v_AMI]=AMI(data, L)
N=length(data);
bins=128; %number of bins used for histogram calculation
epsilon = eps;
data = data - min(data); % making all the data points positive
data = 1+ floor(data/(max(data)/(bins-epsilon))); %scaling the data
v=zeros(L,1); %create a zero vector
overlap=N+1-L;
increment= 1/overlap;
one = ones(overlap,1); %create a column vector with all elements being one
pA = sparse (data(1:overlap),one,increment);
for lag = 0: L -1
pB = sparse(one, data(1+lag:overlap+lag), increment);
pAB = sparse(data(1:overlap),data(1+lag:overlap+lag),increment);
[A, B, AB]=find(pAB);
v(lag+1)=sum(AB.*log2(AB./(pA(A).*pB(B)'))); %Average Mutual Information
end
v_AMI=v;
for i = 1: length(v)-1
if (find((v(i)<v(i+1))&&(v(i)<v(i-1)))) == 1
x(i)=i;
end
end
A=sparse(x);
A=find(A);
tau=A(1); % tau = 1st min(I(time_lag))
end
The code used to call in the function is:
directory_name = uigetdir(pwd,'Select data directory');
directory_name = ([directory_name '/']);
files = dir([directory_name,'*txt']);
namer = struct2cell(files);
if isempty(files)
msgbox('No raw files in this directory')
end
for i=1:length(files)
filename = char(namer(1,i));
data = load([directory_name filename]);
L=32;
[tau,v_AMI]=AMI(data, L); %Find the first minimum average mutual information
end
It is also giving me an error for the line calling in the AMI function : [tau,v_AMI]=AMI(data, L);
How could I resolve these errors? The code is supposed to calculate a tau and v_AMI for each data .txt file containing a single column of numbers.

Accepted Answer

Jon
Jon on 15 Apr 2019
Without having the actual datafile that the program operates it is difficult to fully debug your code. A cursory inspection indicates that the only place that the variable x is assigned is on line 23 which is inside of an if statement. I would suspect that line 23 is never reached by the code (the if statement that it is in is not satisfied). So when you use x as an argument to sparse on line 26 it has never been assigned a value, thus the function or variable x is unknown or undefined at this point.
I would suggest preallocating a properly sized vector of x values all set to zero just before entering the loop, e.g.
x = zeros(length(v)-1,1)
This way if you never satisfy the if statement you will just pass sparse a vector of zeros which should be OK.
Also, you may want to check the logic on your if statement if you think it really should be hitting line 23. I would suggest stepping through your code with the debugger. Then you can check what the values of the various variables are at the point that you enter the if statement. If you don't already know how to do this here's a link. https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
  2 Comments
Lauren
Lauren on 15 Apr 2019
Thank you. I added your suggestion to the function code, just before the for and if loop, and ran it. It gave me an error that said index exceeds matrix dimensions for tau=A(1);
Would it help if I attached the file containing the function?
Jon
Jon on 15 Apr 2019
I think the problem now is that if x contains only zeros, then A = sparse(x), will contain only zeros, and so when you do assign A = find(A) the result is empty. Finally looking for the first element of an empty matrix tau = A(1) generates the error because there is no first element of an empty matrix.
So I think your problem goes back to figuring out why your if statement,
if (find((v(i)<v(i+1))&&(v(i)<v(i-1)))) == 1
is not satisfied.
If you think there may be edge cases where this is never satisfied, then I would also suggest putting in some if statements further down to guard against doing the statement evaluations that do not make sense when all of the x values are zero.
Hope this helps

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!