4

Everyone

I have a problem in dealing with the @folder in Matlab.

I added the path of parent folder where the @folder was contained. But still I could not use the functions in the @folder. That is, for example, there is a function called ttt.m, when I call it directly:

ttt(argument)

it returns: Undefined function or variable.

but when I use help command to look at it, it works:

help ttt

it returns the information of ttt function

I don't understand what is going on here. Please help.

genius0
  • 55
  • 4
  • I realize I may not have answered you question satisfactorily. Do you see a .m file with the same name as the folder? That should be the definition for the class that uses the files in the @folder. You can then use the methods through that class, either through an instance or by a static method. – chappjc Oct 28 '13 at 20:52
  • Thanks chappjc. Yes, I see the .m file in the @folder. I understand your point. but just to make sure: did you mean that I need to call the definition first before I call any other functions in the @folder? – genius0 Oct 28 '13 at 21:23
  • If the method in question is `Static`, then you do not need an instance, otherwise you need to create and instance of the class. See [here](http://www.mathworks.com/help/matlab/matlab_oop/static-methods.html). – chappjc Oct 28 '13 at 21:25
  • Pardon me for not familiar with the concept of 'static'. It seems that after I called the definition first then everything works, at least by far. but still it is very useful comment. Thanks! – genius0 Oct 28 '13 at 21:32
  • Great. You many want to reference [this MATLAB OOP doc page](http://www.mathworks.com/help/matlab/matlab_oop/ordinary-methods.html#brd2n2o-1) for more details. I would have to see the class and method definitions to give an exact code answer, but it seems you got it figured out. – chappjc Oct 28 '13 at 21:34
  • Thanks very much for the link. Now I understand that I'm using some methods that are not static. So I need an instance of the object as the argument for the method. Cheers~ – genius0 Oct 29 '13 at 21:13

1 Answers1

4

Those folders contain code for custom classes or overrides of existing types. You don't put them on the path, or use them directly, but through a class.

Use this type of folder when you want to use multiple files for one class definition. ... An @-folder must be contained by a path folder, but is not itself on the MATLAB path. Place the class definition file inside the @-folder, which can also contain separate method files. The class definition file must have the same name as the @-folder (without the @-sign) and the class definition (beginning with the classdef key word) must appear in the file before any other code (white space and comments do not constitute code).

This is the original way of defining classes; a new way is to have everything in a single file. Here is an explanation of the two methods.

EDIT: If the method in question is Static, then you do not need an instance, otherwise you need to create and instance of the class. See here for information about static methods and an example of instantiating a class to call a method. A more detailed explanation on how to call methods is in the MATLAB OOP documentation.

chappjc
  • 29,576
  • 6
  • 70
  • 120
  • "Old way"? That has certain connotations. As far as I know, it's equally supported – many of The MathWorks' own toolboxes still use it. Are there disadvantages of using the multiple folder scheme? – horchler Oct 28 '13 at 18:04
  • 1
    I don't mean to imply that it's better or recommended by MathWorks, just that it happens to have been introduced after the @ method. However, [others have their opinions about which is better](http://stackoverflow.com/a/2538939/2778484). – chappjc Oct 28 '13 at 18:09
  • That pre-7.6 documentation file itself is obsolete, not necessarily all of the things within. This is [the documentation](http://www.mathworks.com/help/pdf_doc/matlab/matlab_oop.pdf) (PDF) for OOP in R2013b. – horchler Oct 28 '13 at 18:22
  • Fair enough. I removed [the document](http://www.mathworks.com/help/pdf_doc/matlab/pre-version_7.6_oop.pdf) to avoid that confusion. But it's interesting that they bothered to release a document labeled obsolete, just to point to the new docs... that seems odd. – chappjc Oct 28 '13 at 18:24