Why does the Variable Editor display my object incorrectly when I overload the SIZE method in MATLAB 7.9 (R2009b)?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 18 de Nov. de 2009
Editada: MathWorks Support Team
el 13 de Sept. de 2024
I created the following class definition with an overloaded SIZE method:
classdef testclass
properties
values = ones(1,3);
stored;
end
methods
function s = size(hobj)
s = builtin('size',hobj.values);
end
end
end
When I instantiate an object of the class and examine the object in the Variable Editor:
t = testclass;
openvar(t)
I see the following:
val =
<a href="matlab:help testclass">testclass</a>
Properties:
values: [1 1 1]
stored: []
<a href="matlab:methods('bfmsparse')">Methods</a>
This does not occur if I do not overload the SIZE method.
Respuesta aceptada
MathWorks Support Team
el 13 de Sept. de 2024
Editada: MathWorks Support Team
el 13 de Sept. de 2024
The reason that the object is not displayed correctly in the Variable Editor when you overload the SIZE function, is that the Variable Editor calls SIZE with the following syntax:
[height, width] = size(var);
In this case, the overloaded SIZE method does not support this calling mechanism. This causes the call to SIZE to error, which causes the Variable Editor to call DISP instead and display the results.
To ensure that MATLAB functions that call SIZE internally behave correctly, your SIZE method should support all the syntaxes defined in the documentation for SIZE, that a caller of SIZE may reasonably expect to work, namely
d = size(X)
[m,n] = size(X)
m = size(X,dim)
[d1,d2,d3,...,dn] = size(X)
The default SIZE and NUMEL functions should behave consistently with user-defined classes. However, if you change the way size behaves, ensure that the values returned make sense for the intended use of your class.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!