2

Consider the following directory structure and that C:\magic is the current MATLAB folder:

C:\magic
C:\magic\+wand
C:\magic\+hat

Now, wand and hat are MATLAB packages which may be loaded by import wand.* and import hat.*.

Consider that I may want to create an abstract class for hat inside the +hat folder:

% C:\magic\+hat\Hat.m
classdef Hat < handle
    % class implementation ...
end

and some child class:

% C:\magic\+hat\TopHat.m
classdef (Sealed) TopHat < Hat
    % class implementation
    methods
        function this = TopHat()
            this = this@Hat();
        end
    end
end

But when I do:

> import hat.*
> ha = TopHat()

I get the following error:

Error using hat.TopHat
The specified superclass 'Hat' contains a parse error or cannot be found
on MATLAB's search path, possibly shadowed by another file with the same name.

Nevertheless, I can do ha = Hat() with no error.

What is possibly happening and what is the best solution to this issue?

Thanks in advance!

Amro
  • 123,847
  • 25
  • 243
  • 454
Girardi
  • 2,734
  • 3
  • 35
  • 50
  • similar question: [MATLAB : import package for base class](http://stackoverflow.com/q/8475312/97160) – Amro Sep 19 '13 at 02:21

1 Answers1

3

Try

classdef (Sealed) TopHat < hat.Hat

There's no "search-current-package-first"-routine in MATLAB (sorry for the bad name :>). So to refer to a class within a package you always have to carry the package name - even e.g. to refer to a static method of a class within its own classdef.

sebastian
  • 9,526
  • 26
  • 54
  • 2
    yes! I tried that soon after posing this question and it worked... I think MATLAB is quite unorganized, don't you? This issues shouldn't happen... – Girardi Sep 18 '13 at 20:09
  • Compared to natively object-oriented programming languages, things sometimes seem a little artificial. But then again, OOP in MATLAB is still very uncommon - to my impression. – sebastian Sep 18 '13 at 20:16
  • imo you can blame this on the import feature in Matlab... it is kind of primitive. if you think about the syntax that works, Matlab needs to understand the scope, and doesn't look in the same package etc... hopefully Mathworks will make this a bit better one day soon – janh Mar 09 '14 at 11:15