MATLAB function 'nearest' does not work.

Hello,
when I call the function "nearest()" I obtain the error message: "Check for incorrect argument data type or missing argument in call to function 'nearest'." I am using R2021b.
The following toolboxes are installed:
  • MATLAB Version 9.11 (R2021b)
  • Aerospace Toolbox Version 4.1 (R2021b)
  • Deep Learning Toolbox Version 14.3 (R2021b)
  • Financial Toolbox Version 6.2 (R2021b)
  • Image Processing Toolbox Version 11.4 (R2021b)
  • MATLAB Report Generator Version 5.11 (R2021b)
  • Mapping Toolbox Version 5.2 (R2021b)
  • Optimization Toolbox Version 9.2 (R2021b)
  • Parallel Computing Toolbox Version 7.5 (R2021b)
  • Statistics and Machine Learning Toolbox Version 12.2 (R2021b)
The MATLAB documentation for "nearest()" (https://de.mathworks.com/help/fixedpoint/ref/nearest.html) does not say anything about required toolboxes though.
Why does MATLAB not execute tht function "nearest()" correctly?
Thanks for your help.

1 Comment

Stephen23
Stephen23 on 13 Jun 2022
Edited: Stephen23 on 13 Jun 2022
"The MATLAB documentation for "nearest()" (https://de.mathworks.com/help/fixedpoint/ref/nearest.html) does not say anything about required toolboxes though."
You can always identify the toolbox from both the URL and from the contents browser hierarchy:

Sign in to comment.

Answers (2)

I find two functions named nearest in MathWorks products when I search the documentation.
One is the nearest method for graph and digraph objects, which is part of MATLAB. As stated on that page, the first input argument G must be a "graph object | digraph object". If it isn't, you cannot successfully call this method. [You could call the method if one of the later inputs was a graph or digraph object, but if that first input isn't one the method will error out.]
One is the nearest method for fi objects, which is part of Fixed-Point Designer. The one input to this method must be a "Input fi array, specified as scalar, vector, matrix, or multidimensional array." If you try to call this with a normal double precision array as input, it will not work: MATLAB will not call this method.
Which of these methods were you hoping to call and what data did you pass into your nearest() call? Or were you hoping to do something else and hoped that a function named nearest() would let you achieve your goal?

4 Comments

Thanks for you answer!
I was trying to use this method nearest. I was confused because on my other computer I could call this function using a normal double precision array as input. Now, on a new computer it does not work even though I am using the same MATLAB version.
However, I realized that for my intended use-case I can just use round instead.
The workflow you described shouldn't have worked. Perhaps on your other computer you had a nearest.m function that converted the double precision array to fi and then called the method on that object?
which nearest -all
/MATLAB/toolbox/fixedpoint/fixedpoint/nearest.m /MATLAB/toolbox/matlab/graphfun/@digraph/nearest.m % Shadowed digraph method /MATLAB/toolbox/matlab/graphfun/@graph/nearest.m % Shadowed graph method
nearest(1.2)
ans = 1
Huh. That suggests the documentation may need to be updated. I'll note that to the documentation staff for Fixed-Point Designer.

Sign in to comment.

The nearest function is used with graph or digraph objects. Apparently, you are using it with something else.
Without knowing anything at all about what you are using it with, the inpolygon, pdist, pdist2, or knnsearch functions could be appropriate.

1 Comment

I didn’t see Fixed-Point Designer Toolbox in the list in the original post (and I still don’t see it there), so I didn’t link to functions in it.

Sign in to comment.

Tags

Asked:

on 13 Jun 2022

Commented:

on 13 Jun 2022

Community Treasure Hunt

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

Start Hunting!