Fox
Fox

Reputation: 9444

Matlab: Attempt to reference field of non-structure array

I am using the Kernel Density Estimator toolbox form http://www.ics.uci.edu/~ihler/code/kde.html . But I am getting the following error when I try to execute the demo files -

>> demo_kde_3
KDE Example #3 : Product sampling methods (single, anecdotal run)
Attempt to reference field of non-structure array.

Error in double (line 10)
if (npd.N > 0) d = 1;            % return 1 if the density exists

Error in repmat (line 49)
nelems = prod(double(siz));

Error in kde (line 39)
if (size(ks,1) == 1) ks = repmat(ks,[size(points,1),1]); end;

Error in demo_kde_3 (line 8)
p = kde([.1,.45,.55,.8],.05);  % create a mixture of 4 gaussians for
testing

Can anyone suggest what might be wrong? I am new to Matlab and having a hard time to figure out the problem.

Thank You,

Upvotes: 1

Views: 1231

Answers (3)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Repmat calls double and expects the built-in double to be called.

However I would guess that this is not part of that code:

if (npd.N > 0) d = 1;            % return 1 if the density exists

So if all is correct this means that the buil-tin function double has been overloaded, and that this is the reason why the code crashes.

EDIT: I see that @Pursuit has already addressed the issue but I will leave my answer in place as it describes the method of detection a bit more.

Upvotes: 0

Pursuit
Pursuit

Reputation: 12345

Try changing your current directory away from the @kde folder; you may have to add the @kde folder to your path when you do this. For example run:

cd('c:\');
addpath('full\path\to\the\folder\@kde');

You may also need to add

addpath('full\path\to\the\folder\@kde\examples');

Then see if it works.

It looks like function repmat (a mathworks function) is picking up the @kde class's version of the double function, causing an error. Usually, only objects of the class @kde can invoke that functions which are in the @kde folder.

I rarely use the @folder form of class definitions, so I'm not completely sure of the semantics; I'm curious if this has any effect on the error.


In general, I would not recommend using the @folder class format for any development that you do. The mathworks overhauled their OO paradigm a few versions ago to a much more familiar (and useful) format. Use help classdef to see more. This @kde code seems to predate this upgrade.

Upvotes: 1

nrz
nrz

Reputation: 10550

MATLAB gives you the code line where the error occurs. As double and repmat belong to MATLAB, the bug probably is in kde.m line 39. Open that file in MATLAB debugger, set a breakpoint on that line (so the execution stops immediately before the execution of that specific line), and then when the code is stopped there, check the situation. Try the entire code line in console (copy-paste or type it, do not single-step, as causing an uncatched error while single-stepping ends the execution of code in debugger), it should give you an error (but doesn't stop execution). Then try pieces of the code of that code line, what works as it should and what not, eg. does the result of size(points, 1) make any sense.

However, debugging unfamiliar code is not an easy task, especially if you're a beginner in MATLAB. But if you learn and understand the essential datatypes of MATLAB (arrays, cell arrays and structs) and the different ways they can be addressed, and apply that knowledge to the situation on the line 39 of kde.m, hopefully you can fix the bug.

Upvotes: 0

Related Questions