Define shared subfunctions inside a classdef *.m file?
Show older comments
I want to define a relatively small class (small enough that it makes sense to do everything inside of one file). I want to reuse a common subfunction between several methods, and having this subfunction inside of the classdef would make it cleaner, easily portable and automatically private. However, I have not been able to do so. Is there any way to accomplish it?
>> d = dummy;
>>??? Undefined function or variable 'my_pi'.
>>
>>Error in ==> dummy>dummy.dummy at 10
>> obj.dummy = my_pi();
dummy.m:
classdef dummy
properties
value;
end
methods
function y = my_pi()
y = 3.141592;
end
function obj = dummy()
obj.dummy = my_pi();
end
end
end
Accepted Answer
More Answers (1)
David Young
on 21 Feb 2011
Non-static methods require an object as the first argument, and as my_pi is not declared static and doesn't have an argument, it isn't found.
Rather than putting my_pi at the end of the file, I would include it properly in the class by declaring it to be static:
classdef dummy
properties
value;
end
methods
function obj = dummy()
obj.value = dummy.my_pi();
end
end
methods (Static)
function y = my_pi()
y = 3.141592;
end
end
end
You need to use the class name in the call to the static method. I've also corrected the assignment in the constructor: I assume that you intented to set the value property.
You could also set my_pi's access to private if you want it only to be visible from other methods of the class.
8 Comments
Knut
on 21 Feb 2011
David Young
on 21 Feb 2011
But you accepted the former post!
per isakson
on 21 Feb 2011
The static method is certainly the solution preferred from an OO point of view.
Jiro Doke
on 21 Feb 2011
@David or @per isakson,
Can you explain why a static method is better here than a subfunction at the end? If this function is private and only used internally, wouldn't it be better to declare it as a subfunction?
David Young
on 22 Feb 2011
Jiro: First, you can set the access of a method to private - so the private access is explicit, not implicit as for a subfunction. Second, if a file starts "classdef" I expect the file to finish with the matching "end" - it seems messy to me to have bits and pieces hanging around after that. Third, it's cleaner and more consistent to stay within the OO paradigm, at least within a single m-file. Third, because the static method requires the class name to call it, you know exactly where it's to be found, and there's no possibility of getting mixed up with another function of the same name. Fourth, you can easily make the method public if you want to by changing its attributes, whereas the subfunction would need to be moved into a separate file. So overall, it's about cleanness and consistency.
per isakson
on 22 Feb 2011
"Better" can mean different things. I second David's description why a static method is better. I might add that programmers (other than yourself) with experience from other/real OO-languages will understand your code in less time. However, I often use the construct with a subfunction because it helps me make the line with the call more readable. The class typically has a long descriptive name, which would dominate and often force me to use continuation lines. This is more true in cases when the subfunction is called several times.
David Young
on 22 Feb 2011
Agreed - it does depend on the programming context. Also, I see I can't count. Oh well...
Knut
on 23 Feb 2011
Categories
Find more on Handle Classes 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!