@folder and +folder
Asked Answered
H

1

35

What is the meaning of the following folder names in MATLAB?

  • @folder
  • +folder

I've created a class Tata.m which uses the classdef syntax. Should I put it in an @folder or a +folder?

I've looked at the documentation but it is not really clear in which cases the @folder should be used and in which cases the +folder should be used.

Hinayana answered 20/3, 2012 at 5:42 Comment(0)
B
41

The +folder piece is a MATLAB package folder. If you place Tata.m in a location like +folder/Tata.m, it will be known to MATLAB as the class folder.Tata. If you place it in a folder like someOtherFolder/Tata.m, or someOtherFolder/@Tata/Tata.m, it will be known to MATLAB as Tata.

It can be useful to place a classdef file in a class directory like @Tata to allow you to put the definition of some (or all) methods in separate files.

The doc has more details.

EDIT: To attempt to clarify the @ directories: historically, a class Tata with methods methodOne and methodTwo would require the following files:

somePlaceOnThePath/@Tata/Tata.m
somePlaceOnThePath/@Tata/methodOne.m
somePlaceOnThePath/@Tata/methodTwo.m

In the "new" object system, you can still use the layout above without modification. At the other extreme, you can place the entire implementation of Tata in a single classdef block in:

somePlaceOnThePath/Tata.m

If you have some large methods, or want to split up the implementation of the class Tata into several files to make parallel development simpler, you can take use a classdef like this:

%# somePlaceOnThePath/@Tata/Tata.m:
classdef Tata
    methods
         result = methodTwo(obj, arg)

         function methodOne(obj)
             disp('hello from methodOne');
         end
    end
end

And also

%# somePlaceOnThePath/@Tata/methodTwo.m:
function result = methodTwo(obj, arg)
% do stuff with obj and arg
end

Strictly speaking, the advance declaration of methodTwo in the classdef is optional because it's using the default access specifiers. If you wanted to have methodTwo be a private method, you could place it in a methods (Access = private) block.

Bothwell answered 20/3, 2012 at 7:16 Comment(3)
ok, thanks Edric, now, I understand the +folder but not the @folderHinayana
Documentation link is broken and I can't quite figure out what it was supposed to be. I'm also still struggling to understand the meaning and significance of a "MATLAB package folder".Reportorial
Hm, I'm not 100% sure now either, perhaps it was meant to be mathworks.com/help/matlab/matlab_oop/… or mathworks.com/help/matlab/matlab_oop/…Bothwell

© 2022 - 2024 — McMap. All rights reserved.