Reputation: 193
I'm trying to modify someone else's code that uses global variables and keep getting an error that I can't figure out. This is my first time using global variables, so I suspect it has to do with that. I'm getting this error:
Attempt to reference field of non-structure array.
Error in likelihood (line 35)
X=ModelInfo.X;
This is the code I run:
clearvars -global
clearvars
global ModelInfo
%Pull in data from MySQL database
ModelInfo.X=..... %76x2 double type
ModelInfo.y=..... %76x1 double type
addpath('C:/.../Sampling Plans');
addpath('C:/.../Constructing a Surrogate');
%Number of variables
k=size(ModelInfo.X,2);
%Number of sample points
n=size(ModelInfo.X,1);
%Set upper and lower bounds for search of log theta
UpperTheta=ones(1,k).*2;
LowerTheta=ones(1,k).*-3;
%Run GA search of likelihood
[ModelInfo.Theta,MinNegLnLikelihood]=ga(@likelihood,k,[],[],[],[],LowerTheta,UpperTheta);
...which references an outside function likelihood (written by someone else):
function [NegLnLike,Psi,U]=likelihood(x)
global ModelInfo
X=ModelInfo.X;
y=ModelInfo.y;
theta=10.^x;
p=2;
n=size(X,1);
one=ones(n,1);
% Pre-allocate memory
Psi=zeros(n,n);
% Build upper half of correlation matrix
for i=1:n
for j=i+1:n
Psi(i,j)=exp(-sum(theta.*abs(X(i,:)-X(j,:)).^p));
end
end
% Add upper and lower halves and diagonal of ones plus
% small number to reduce ill-conditioning
Psi=Psi+Psi'+eye(n)+eye(n).*eps;
% Cholesky factorisation
[U,p]=chol(Psi);
% Use penalty if ill-conditioned
if p>0
NegLnLike=1e4;
else
% Sum lns of diagonal to find ln(abs(det(Psi)))
LnDetPsi=2*sum(log(abs(diag(U))));
% Use back-substitution of Cholesky instead of inverse
mu=(one'*(U\(U'\y)))/(one'*(U\(U'\one)));
SigmaSqr=((y-one*mu)'*(U\(U'\(y-one*mu))))/n;
NegLnLike=-1*(-(n/2)*log(SigmaSqr) - 0.5*LnDetPsi);
end
UPDATE:
Figured out the problem. I declared ModelInfo as a global variable at the beginning, and then within the %Pull in data from MySQL database
section I created a database connection and then cleared some variables we wouldn't need anymore using the clearvars -except
command, but I didn't include ModelInfo in that list. After that I put data into ModelInfo. Since it's global, I guess that deleted either the local copy or the global copy (not quite sure), but it still appeared in the list of variables in the workspace and isstruct(ModelInfo)
still returned 1...so visibly there was nothing to indicate an issue other than the error.
Upvotes: 0
Views: 1295
Reputation: 26069
If that is indeed the code you run:
%Pull in data from MySQL database
ModelInfo.X=..... %76x2 double type
ModelInfo.y=..... %76x1 double type
I doubt it has anything to do with global variables. You should have some data of some sort instead of the 5 dots .....
in order to make any code work (as the comment that is placed above indicate "pull data from..."). I'd also try to make this data in the form that the comments suggest if it isn't of the same class, i.e. make it double
and of the size mentioned.
For example, try to see if replacing these lines with this makes your code work
ModelInfo.X=rand(76,2) %76x2 double type
ModelInfo.y=rand(76,1) %76x1 double type
Upvotes: 1