implicit conversion doesn't work with some functions
1 view (last 30 days)
Show older comments
classdef A < double
end
When I pass this object into signal processing toolbox function, interp1, as the third argument, I'm greeted with:
Error using matlab.internal.math.interp1
Input coordinates must be of type double or single.
Why is the object not being implicitly converted to a double?
Thanks.
2 Comments
Steven Lord
on 19 Jan 2024
The interp1 function is in MATLAB, not Signal Processing Toolbox, unless it has been overloaded for a class that's defined in that toolbox. The output of which suggests that it has not been.
which -all interp1
I see there's a function named interp in Signal Processing Toolbox, but that's different from interp1.
which -all interp
Can you show a small sample of code that we could run to reproduce this error? I suspect I know what's going on, but I'd like to see your actual code to confirm.
Accepted Answer
More Answers (2)
Steven Lord
on 19 Jan 2024
I assume you're basing your expectation about how interp1 should work on either this documentation page (specifically the "Behavior of Built-In Functions with Subclass Objects" section) or this other documentation page. Those pages are correct in the behavior they describe, but you're overlooking a key point.
Built-in methods
which sin
The sin function is a method defined for the double class. That means that when you call sin on your object, because it operates on data values the applicable parts of the two documentation pages to which I linked are:
"Operations on data values return objects of the superclass. For example, if you subclass double and perform addition on two subclass objects, MATLAB adds the numeric values and returns a value of class double." (from the first of the documentation pages to which I linked) and
"When you call a built-in data value method on a subclass object, MATLAB uses the superclass part of the subclass object as inputs to the method. The value returned is same class as the built-in class." (from the second of those documentation pages, which in fact uses sin as its example.)
Here's what I see when I call sin on an instance of your subclass.
>> x = A(5)
x =
A:
double data:
5
>> y = sin(x)
y =
-0.9589
>> whos x y
Name Size Bytes Class Attributes
x 1x1 8 A
y 1x1 8 double
Functions included as part of MATLAB
which interp1
The interp1 function is not a method of any class. It's a plain old function file. As such, it doesn't get the "superclass part of the subclass object" -- it gets the whole subclass object. For some functions, this is perfectly fine. As one example of a function that operates as you might expect on the subclass object, the factorial function calls a number of built-in functions (including isa, which gets the whole object because it needs that information) and can return an instance of the class.
which factorial
which isa
Aside: isa falls into the "Comparing objects or testing for inclusion in a specific set returns logical or built-in objects, depending on the function. Functions such as isequal, ischar, isobject work with subclass objects the same as they work with superclass objects." category just like isobject does.
factorial function behavior on subclass
Here's what I got in the Command Window when I called factorial on an instance of your subclass:
>> x = A(5)
x =
A:
double data:
5
>> y = factorial(x)
y =
A:
double data:
120
interp1 behavior on subclass
But interp1 doesn't just check isa, it is stricter for efficiency's sake. So it gets the whole subclass object (not just the superclass piece) and that subclass doesn't match its stricter check. So MATLAB throws the error.
If you feel strongly that interp1 should use a less strict check, that it should allow subclasses of double and/or single to flow through it, please send an enhancement request to that effect (with a description of how you would use this functionality if it were present) to Technical Support directly using this link.
0 Comments
Walter Roberson
on 19 Jan 2024
classdef A < double
means that A takes in double and returns objects of class A.
q = interp1(X,Y,a,"spline",'extrap');
You then pass that object of class A into interp1 . interp1 does not know how to process objects of class A.
0 Comments
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!