implicit conversion doesn't work with some functions

1 visualización (últimos 30 días)
Dan
Dan el 19 de En. de 2024
Respondida: Steven Lord el 19 de En. de 2024
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 comentarios
Steven Lord
Steven Lord el 19 de En. de 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
/MATLAB/toolbox/matlab/polyfun/interp1.m /MATLAB/toolbox/matlab/datatypes/datetime/@datetime/interp1.m % datetime method /MATLAB/toolbox/matlab/datatypes/duration/@duration/interp1.m % duration method /MATLAB/toolbox/nnet/deep/+deep/+internal/+recording/@RecordingArray/interp1.m % deep.internal.recording.RecordingArray method /MATLAB/toolbox/nnet/deep/@dlarray/interp1.m % dlarray method /MATLAB/toolbox/parallel/gpu/@gpuArray/interp1.m % gpuArray method /MATLAB/toolbox/parallel/parallel/@codistributed/interp1.m % codistributed method
I see there's a function named interp in Signal Processing Toolbox, but that's different from interp1.
which -all interp
/MATLAB/toolbox/signal/signal/interp.m /MATLAB/toolbox/shared/controllib/engine/@DynamicSystem/interp.m % DynamicSystem method
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.
Dan
Dan el 19 de En. de 2024
Steven,
I stand corrected on the containing toolbox. Thank you.
I've attached a routine and a class definition that cause the error.
Thanks for your response.
Dan

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 19 de En. de 2024
Editada: Matt J el 19 de En. de 2024
interp1 is not a method of class double, so it does not have to accommodate double's child classes.
ismember('interp1',methods('double'))
ans = logical
0

Más respuestas (2)

Steven Lord
Steven Lord el 19 de En. de 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
built-in (/MATLAB/toolbox/matlab/elfun/@double/sin) % double method
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
/MATLAB/toolbox/matlab/polyfun/interp1.m
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
/MATLAB/toolbox/matlab/specfun/factorial.m
which isa
built-in (/MATLAB/toolbox/matlab/datatypes/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.

Walter Roberson
Walter Roberson el 19 de En. de 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.

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by