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

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

Input file names need to be quoted if they are literal strings.
mean_percentile=MCSimulation(2000,20,0.99,'r.xls')

11 Comments

I'm sorry I'm not a good matlab user... i just need this code to do a model for my thesis. However I've input as you both said and I had a new error...
mean_percentile=MCSimulation(2000,25,0.99,'r.xls')
Warning: Could not start Excel server for import, 'basic' mode will be used. Refer to HELP XLSREAD for more
information.
> In xlsread at 176
In MCSimulation at 13
??? Attempted to access database(1,2); index out of bounds because size(database)=[3524,1].
Error in ==> MCSimulation at 19
a=normrnd(database(i,1),database(i,2),1,num_sim);
that i guess it means errors are in line n.176 of xls function:
else
try
Excel = actxserver('excel.application');
catch exc %#ok<NASGU>
warning('MATLAB:xlsread:ActiveX',...
'Could not start Excel server for import, ''basic'' mode will be used. Refer to HELP XLSREAD for more information.');
basicMode = true;
end
end
and in line n.13 of my code:
database=xlsread(input_file);
since i'm not that into matlab language, i don't understand how i could fix it... i've read on the web that xlsread has problems with mac... but the friend who gave this code to me used a mac... it would be great to use a normal variable in my workspace instead of a xls file... do you think it is possible?
Your input file only has one non-blank non-NaN non-text column. (for the first output argument, the numeric columns, xlsread removes all leading and trailing rows and columns that have no discernible numbers.)
You can try by hand and see what you get:
[numericarray, textarray, rawarray] = xlsread('r.xls');
i get an epty text array variable <0x0 cell> and a numeric array of 3524x1 cells, that are my returns, the only datas in the file...
the the raw array is the same returns between [], such as:
[ -1.2367 ]
[ 0.5563 ]
and so on...
That appears to be only one column to me. Are you sure the original data is more than one column? If it is only one column then database(i,2) is going to try to access a non-existent column.
I don't understand why you tried "Excel = actxserver('....." and it threw the exception (because we see the error message in your catch block), yet you have a cell array of 3524x1 cells. If it threw the exception, then why do you have anything in the cell array???
Second question - if you open up this workbook in Excel, how many columns and rows of numbers do you see? And how many columns and rows of pure text do you see?
If Excel is not installed or it is not MS Windows, then the warning gets thrown and the code for basic mode is used.
Well, I do have Excel, but the computer I use is a mac... there's no way to change the code and avoid xlsread by using a simple variable of the workspace?This morning I've tried changing my r.xls putting two columns, and it repeats the same errors
> In xlsread at 176
In MCSimulation at 13
it says that basic mode will be used, and then he generates a variable with 3524x1 cells but many NaN cells...
Mac will always use basic mode for xlsread() on OS-X or Linux. Whether you can substitute anything for xlsread() depends on the type of .xls file you are using. Have you tried exporting from excel into csv format and reading that?
Note that if your .xls file has Excel constructs such as macros that are too complicated for "basic mode" (which works on comma separated value text format, and the binary equivalent of those, but not so much else), then the entries might be read as NaN and then the trailing NaN column trimmed off for the output numeric array array.
csv is not a recognized format... :( my xls file is super easy, latest version is column A: prices, column B: returns. No macro, no colors, not even names of the variable in cells A1 and B1... and I still get several NaN...
It is not possible to use:
mean_percentile=MCSimulation(2000,20,0.99,'returns')
where returns is not a excel file but the imported variable of my returns? i just want to delete xlsread from the code and refers to a normal variable of my workspace... i think it is the only solution... (maybe... well... i hope...)
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
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)

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

We might also need to know the MATLAB version.
I write:
mean_percentile=MCSimulation(num_sim,seed,p,input_file)
putting num_sim=2000, seed=20, p=0.99 and input_file=r.xls, and I get this:
mean_percentile=MCSimulation(2000,20,0.99,r.xls)
???Attempt to reference field of non-structure array
mmmm the version I use is 7.10.0.499 (R2010a) for Mac
thanks very much
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.

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

1 Comment

Read this and start your own question with the full error message pasted in.

Sign in to comment.

Categories

Products

Tags

Community Treasure Hunt

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

Start Hunting!