Question

I have a project in matlab with the following directory structure:

+namespace\
    @class1\
        class1.m
    @class2\
        class2.m
mainfile.m

in class1.m I have something like the following

classdef class1

    %readonly variables
    properties(GetAccess = 'public',SetAccess = 'private')
        forename;
        lastname;
        middlename;

    end

    properties(Constant = true)

        %in centipascals
        p1 = class2(param1,param2); %this is the part I need to work

    end

    methods(Access = public)

        function this = class1(fname,lname,mname)

            this.forename = fname;
            this.lastname = lname;
            this.middlename = mname;

        end
    end
end

I can't seem to get this class working. Class1 doesn't recognize the constructor of class2 (probably because something isn't being imported correctly). How do I import class2 or what do I need to do in order to have other class instances as member variables?

Was it helpful?

Solution

In Matlab, you need to fully qualify references to classes in a namespace, even from other classes within that same namespace. Like this.

classdef class1
    properties (Constant = true)
        %in centipascals
        p1 = namespace.class2(param1,param2);
    end
end

You can import other classes from the same namespace, but imports only work at a per-function level, and don't work in properties blocks at all AFAIK, so it won't work in this specific case, and may be more trouble than it's worth elsewhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top