"Attempt to reference field of non-structure array" error message

1 view (last 30 days)
I have this code to calculate Value-at-Risk with Monte Carlo simulation
function mean_percentile=MCSimulation(num_sim,seed,p,input_file)
database=xlsread(input_file);
[row,col]=size(database);
percentili=zeros(row,seed);
for j=1:seed
for i=1:row
a=normrnd(database(i,1),database(i,2),1,num_sim);
percentili(i,j) = prctile(a(1,:),p);
clear a
end
randn('state',sum(100*clock));
end
mean_percentile=zeros(row,1);
for i=1:row
mean_percentile(i,1)=-mean(percentili(i,:));
end
clear database
end
but then when i try using it I have this error message:
Attempt to reference field of non-structure array.
Can anyone help me please?
thanks very much!

Accepted Answer

Walter Roberson
Walter Roberson on 8 Sep 2013
Input file names need to be quoted if they are literal strings.
mean_percentile=MCSimulation(2000,20,0.99,'r.xls')
  11 Comments
Turi
Turi on 9 Sep 2013
could it be the problem also in this function:
a=normrnd(database(i,1),database(i,2),1,num_sim)
I mean, maybe he expects me to input mean and st dev somewhere in my excel spreadsheet? According to this page http://www.mathworks.it/it/help/stats/normrnd.html first 2 variables are distribution mean and st dev
Turi
Turi on 9 Sep 2013
HI GUYS!!!!
After many... really MANY, attempts... IT WORKED. So, the two columns where the moving average of my returns and the standard deviation of the moving window. So, for 3524 returns, with a moving window of 100 observation i get my 3424 simulated VaR for each day.
Then i've easily tested the model on excel and... it sucks! That's coherent with the result of my thesis (and with many article in the economic literature) about non-normal distribution of the returns.
Maybe I'll try with t-distribution... even thought that would not be coherent with Markov chain theory...
THANKS VERY MUCH
have a good week :) :) :)

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 7 Sep 2013
I don't see any structure in that code. There is no expressions that looks something like structureName.field. You'll need to give the exact error message - all the text in red, don't snip or paraphrase.
  4 Comments
Image Analyst
Image Analyst on 8 Sep 2013
Edited: Image Analyst on 8 Sep 2013
It thinks r is a structure, and that xls if a field (member) of the structure called r. Your function expects a filename, which should be a string, 'r.xls', not a structure field r.xls, unless that member is also a filename string. In your case you clearly just forgot to wrap your filename in single quotes to make it a string. So pass in 'r.xls' and it should work, as Walter already pointed out. This clearly illustrates the delay in providing you an answer when you don't give us the full error message but just give us a small snippet of the full error message. You could have had this solved yesterday if you had just given us the full error message.

Sign in to comment.


vahide jafari
vahide jafari on 23 Feb 2015
Hi every body I wanna use function som_autolabel(sTo,sFrom,mode,inds) but it gives me this error:Attempt to reference field of non-structure array.
I do not know for what it gives this. please help me i need it.
Best
vahide

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!