0

In Matlab I have the package "+mypackage". All the following definitions are under this folder/package.
MyClass.m

classdef MyClass
    properties
        prop1 MyProperty
    end
    
    methods (Static)
        function obj = with_five()
            obj = mypackage.MyClass(5);
        end
    end
    
    methods
        function obj = MyClass(num)
            obj.prop1 = mypackage.MyProperty(num);
        end
    end
end

MyProperty.m

classdef MyProperty    
    properties
        num double
    end
    
    methods
        function obj = MyProperty(val)
            obj.num = val;
        end
    end
end

So in MyClass.m I define the prop1 property to be an object of class MyProperty.
Now I would like to create objects with this script.

import mypackage.MyClass

class_test1 = MyClass(5);
class_test2 = MyClass.with_five();

When I run it I get following errors:

Error using Testpackage (line 3)
Error defining property 'prop1' of class 'mypackage.MyClass':
Class named 'MyProperty' is undefined or does not support property validation.

When I remove the property definition from MyClass.m it works. Is there a way to define properties to be an object of an own class in Matlab?

klaushipp
  • 75
  • 6
  • 2
    You just need to define `prop1` as class `mypackage.MyProperty` right? Using `import` is pretty nasty because you're led to believe that everything is in the public namespace when it isn't within different scopes, if you're using a package then it's better to fully-qualify the names – Wolfie Jul 16 '21 at 10:27
  • Oh that was easy. Thank you! I thought I tried that already. Defining ```prop1``` with ```mypackage.MyProperty``` works! Do you mean the import in my script is not a good idea? – klaushipp Jul 16 '21 at 10:35
  • Well it's not a good idea for exactly the issue you've encountered. Namespaces are good because they avoid [shadowing](https://stackoverflow.com/questions/15939754/what-does-shadows-it-in-the-matlab-path-mean-how-to-do-it-in-a-file), and allow you to be explicit in which function you're calling. By using `import` you are dumping everything into the main namespace from that package, but only within the scope of your script/function - which can be confusing when working across several scopes/files. You also obviously lose the advantages which namespacing offers – Wolfie Jul 16 '21 at 10:57

1 Answers1

1

Thank you Wolfie your Help! I changed the definition for prop1 to mypackage.MyProperty.
My Class definition looks now like this:

classdef MyClass
    properties
        prop1 mypackage.MyProperty
    end
    
    methods (Static)
        function obj = with_five()
            obj = mypackage.MyClass(5);
        end
    end
    
    methods
        function obj = MyClass(num)
            obj.prop1 = mypackage.MyProperty(num);
        end
    end
end

Its necessary to point to MyProperty inside my package mypackage.
Otherwise Matlab is not able to find it.
I found it in the Matlab help: https://de.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html?s_tid=srchtitle#brfynt_-3

klaushipp
  • 75
  • 6