MATLAB: Struct contents reference from a non-struct array object

struct

hello
I am new to Matlab and I am trying to minimize the following function with fmincon:
function [ CRRA ] = CRRA(x)
tur = load('tur.mat');
wei = load('wei.mat');
ret = tur.tur *x';
first = 1 + ret;
util = ((first.^(-85))/(-85));
wut = wei. * util;
CRRA = -mean(wut);
end
tur is a 140×500 matrix, x is what I am looking for, i.e. a 1×500 matrix, and wei is a 140×1 matrix. The problem is the line
wut = wei. * util
where I want to multiply wei with util element by element. I get the message:
Struct contents reference from a non-struct array object
with reference to this line. I also tried wut = times (wei, util) but obviously didn't work.
When I copy wut = wei. * util; and paste it in the command editor it calculates wut correctly. I would really appreciate any help.
Thank you

Best Answer

function [ CRRA ] = CRRA(x)
tur = load('tur.mat');
wei = load('wei.mat');
The preceding two lines load the MAT-file each and every time this function is called. Instead load the data once before calling fmincon and pass them into your objective function as additional parameters.
When you do this you can pass in tur.tur and wei.wei as the parameters, eliminating the need to extract those fields from each struct array each time the function is executed. You successfully extracted the field from tur on this line:
ret = tur.tur *x';
first = 1 + ret;
util = ((first.^(-85))/(-85));
but you forgot to extract the field on this line (in which I've replaced ". *" with ".*" to correct the original error) which led to the new error.
wut = wei.* util;
CRRA = -mean(wut);
end