2

Suppose i have a lot of source files, i want to organize them in folders-tree structure.

Is it possible for me to have several files with same name and use every of them from place i need or i must have all functions and classes with different names?

In C++ i have #include to introduce functions i need, that's here?

Just to illustrate:

.\main.m
.\Algorithms\QR\Factory.m    % function Factory
.\Algorithms\QR\Algorithm.m  % function Algorithm
.\Algorithms\SVD\Factory.m   % function Factory
.\Algorithms\SVD\Algorithm.m % function Algorithm
Yola
  • 18,496
  • 11
  • 65
  • 106
  • 2
    This question might help: http://stackoverflow.com/questions/1277613/how-do-i-emulate-include-behaviour-in-matlab but it looks like Mathworks has removed the docs for this :/ – Dan Oct 14 '14 at 06:38
  • 1
    You may want to specify this question a little. Isthe problem that you want to write multiple functions in the same file and use them separately? Or do you want to create multiple files with the same names and each file containing one function? Or maybe a mix. Or do you want to give many files the same name and let them contain functions with different names? – patrik Oct 14 '14 at 07:38
  • OK so then you want to do something similar to which in c++ would be `#include "Algorithms\QR\Factory.m"... #include "\Algorithms\SVD\Factroy.m"`? – patrik Oct 14 '14 at 08:02
  • @patrik , yes, include one of them. I know about `addpath` and `rmpath`, but it seems not very convinient way. – Yola Oct 14 '14 at 08:26
  • @Dan: doc links have been reorganized a few releases ago, but the solution is still valid: http://undocumentedmatlab.com/blog/changes-in-the-online-doc-urls – Amro Oct 14 '14 at 08:36

2 Answers2

5

MATLAB has support for namespaces. So in your example you would create the following:

C:\some\path\main.m
C:\some\path\+algorithms\+qr\factor.m
C:\some\path\+algorithms\+svd\factor.m

(Note: Only the top-level package folder's parent folder must be on the MATLAB path, i.e: addpath('C:\some\path'))

Then you could invoke each using its fully qualified name:

>> y = algorithms.qr.factor(x)

>> y = algorithms.svd.factor(x)

You could also import the package inside some scope. For example:

function y = main(x)
    import algorithms.svd.*;
    y = factor(x)
end
Amro
  • 123,847
  • 25
  • 243
  • 454
  • You may want to add that this is not exactly the same as what he writes in the example, since a matlab function is not the same a header. A header contains function definitions and if two different headers use the same namespace (or are global), then you would have a conflict in c/c++ as well. Otherwise, really good answer. I was completely unaware of this. – patrik Oct 14 '14 at 08:47
  • this is similar to [namespaces](https://en.wikipedia.org/wiki/Namespace) in [C++](http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm) or packages in Java. This is just a way to organize related functions and classes and avoid name clashes.. Obviously there is no `#include` in MATLAB, the closest thing is `addpath` (although it has its own set of rules to determine [precedence](http://www.mathworks.com/help/matlab/matlab_prog/function-precedence-order.html)) – Amro Oct 14 '14 at 08:56
  • After i moved my files into SDK/Algorithms, it can't find them with command `SDK.Algorithms.QR.Modified(A)`. I got *Undefined variable "SDK" or class "SDK.Algorithms.QR.Modified".* – Yola Oct 14 '14 at 08:59
  • 2
    @Yola: did you notice the `+` sign at the beginning of each package folder name? You might wanna first go through the documentation page I linked.. – Amro Oct 14 '14 at 09:01
1

To understand the problem I need to explain some difference between the relation of c++ source and header files, and to .m files.

First: In matlab, you can only run the function which is defined highest up in the .m file. This file defines the top of the hierarchy. Then subfunctions can be implemented in the same m file, but these can only be used inside the same .m file.

Secondly: In addition to this matlab searched the include path for a specific filename and assume that the function inside the file will have the same name. You will notice this by a warning if you define the function with another name than the filename. The thing here is that you cannot have 2 matlab functions with the same name if all functions are global. This would be the same as if you would have 2 functions with the same name and in the same namespace in c++.

Note: The include path in matlab can typically be done with a hardcoded file in the to folder of your program. This function uses the matlab funcion addpath.

This is a fundamental difference to c/c++ where multiple functions are allowed to be defined in the same source file. Then the header file select what source code that you implement in the program, by providing the function definitions. The important thing here is that the header is completely disconnected from the function names, which they are not in matlab. This means that the analogy in your examples is not exactly accurate. The proposed thing by you is to "include" 2 functions with the same name. This is not possible either c/c++ (assuming the functions uses the same namespace or ar global) or in matlab.

Example: If the headers topFolder/foo/bar.h and topFolder/baz/bar.h would both contain the function void myDup(int a) and both headers uses the same namespace (or are global), then that would generate an error.

However, if the functions are only used by a limited number of other functions, then a function, eg. Factory.m, could be included as private functions in different folders. That would also mean that only this folder can access it. It is also possible to use matlab namespace as said in Amro's answer.

Community
  • 1
  • 1
patrik
  • 4,506
  • 6
  • 24
  • 48