Adding +Quandl to Matlab path - matlab

I'm having a lot of issues with Quandl in Matlab lately.
I tried adding +urlread2 to the Matlab search path but I got the message "you cannot add method folders or private folders to the matlab search path." I don't know why I'm getting this message since I'm supposed to add +urlread2 to the path to use Quandl.
Thoughts?

For class directories you shall only add the parent folder containing them to the path: MATLAB will then recognize the classes below automatically. This is what the message you get "means".
See for example how the genpath function is implemented.

Related

Matlab paths and file handling with packages and class folders

So I have the following (simplified) project structure.
scripts/
client.m
src/
+estimators/
#estimator1/
3party/
shell_script.sh
estimator1.m
Basically, what I try to do is, calling estimator1.m from client.m. This is working very well, no issues. However, from within estimator1.m I try to call shell_script.sh. The problem, relative paths do not work since it is always looking from the path client.m is called.
What I tried from estimator1.m is:
pathToScript = fullfile('3Party','shell_script');
system(pathToScript);
I would really like to avoid absolute paths since I'd like this package to be used by others as well. So hardcoding paths to my home directory is not good.
Use mfilename('fullpath') to get the full path of the currently executing file. Use fileparts to get just the directory part. Putting it together,
scriptdir = fullfile (fileparts(mfilename('fullpath')), '3Party');
Here is an additional answer to the one by #Edric. My second issue was that I wanted to implement the feature in the parent class since it is a functionality used by all child classes. The proposed answer is perfect when you have the function implemented in the class itself. However:
scriptdir = fullfile (fileparts(mfilename('fullpath')));
would always return the location of my parent class. So I implemented following in the parent class to retrieve the correct location in child classes:
function classdir= getClassDir(obj)
[classdir, ~, ~] = fileparts(which(class(obj)));
end

Find missing but required files for running a script

While deploying packages of Matlab code using Matlab 2015 I encountered the problem of gathering all required files from my repository to run a certain file or set of files. Matlab has a method for simplifying this process, matlab.codetools.requiredFilesAndProducts.
However, sometimes some files are missing in the repository (either because I got a package from someone else who was not that careful or because it was not checked in in the repository).
When running the code one would get of course an error:
Undefined function or variable 'XXX'.
However, this may take long to fix (running takes long, you would have to repeat for every missing file). Therefore I thought to use the function above. Unfortunately, it only lists existing files in the output (I tested this). Functions that are called from your code, but that are not present in the current path are omitted by matlab.codetools.requiredFilesAndProducts.
My problem: I would like to get a list of all files that are required by running a certain file but are not present in the current path so that I can find them and add them to my collection.
I know that this must be an iterative process because the missing files could themselves call other missing files and I know that there would be false positives, some of these items could be unknown variables instead, and I know that the missing files would only have a name, no path (of course).
My question: What is the easiest way to find a list of potentially missing files of my code in one go?
Please note that function depfun has been removed in recent versions of Matlab.

How to reset #class folder to native state

I have inadvertently messed around with my matlabroot folder and now some functionality is broken. How do I reset the state of the folder so that everything works again?
While trying to solve this question, I added a new method (nansubset.m) to the #table folder ([matlabroot,'\toolbox\matlab\datatypes\#table\*']). That did not work so I deleted it and moved it to my Matlab path as suggested by #TroyHaskin's answer. Everything worked fine until I restarted Matlab. Now I am getting the error message:
Error using table/nansubset
Previously accessible file "C:\Program
Files\MATLAB\R2015b\toolbox\matlab\datatypes\#table\nansubset.m" is now inaccessible.
I have tried clear classes and clear java and restarting Matlab, to no avail.
EDIT:
I also found this link on the Mathworks site, which led me to doc toolbox_path_cache that in turn suggests using rehash toolboxcache. I did this and now there is a new error message:
Error using table/nansubset
Method 'nansubset' is not defined for class 'table' or is removed from MATLAB's search path.
However, which nansubset returns the right file from a directory on my Matlab path.
I have fixed my problem. Essentially, following the instructions from here, we are led to the helpfile for toolbox_path_cache, which states:
When you add or remove files in matlabroot/toolbox folders by some
other means, MATLAB might not recognize those changes.
...
Update the cache so MATLAB recognizes
the changes you made in matlabroot/toolbox folders.
To update the cache, type rehash toolbox and rehash toolboxcache. After this, it was necessary to restart Matlab to fix the problem

Using MATLAB, why does something like fts.data work in one directory but not another?

I am working with the financial tooldbox that has a type called FINTS. If I copy some code out of its toolbox directory to customize it, when I try do do something like fts.data, `I get
The specified field, 'data', does not exist in the object.
But the same thing works fine in the MATLAB library directory. They are both in my path, so what else do I need to change?
I think, but I haven't checked the documentation on this one, that it is a peculiarity of MATLAB that the class FINTS must be defined in the directory #fints. So if you want to extend the class, you have to put your code into that directory. And if you want to work on a class MYFINTS, you need to put the code into directory #myfints.
OK, I figured it out. MATLAB defines class methods in what it calls method directories which are named after the class. So in this case, the class is fints, so all its methods are in #fints. All I had to do was make a new directory in my own workspace called #fints, and it will become another class method of fints. You can see all the methods a class has by calling what className.
Make sure the path is specified from the root directory, and not relative.
For instance
addpath 'c:\...\...\MATLAB\mytoolbox
not
addpath 'mytoolbox'
the latter will break if you change your working directory

What is the closest thing MATLAB has to namespaces?

We have a lot of MATLAB code in my lab. The problem is there's really no way to organize it. Since all the functions have to be in the same folder to be called (or you have to add a bunch of folders to MATLAB's path environment variable), it seems that we're doomed have loads of files in the same folder, all in the global namespace. Is there a better way to organize our files and functions? I really wish there were some sort of module system...
MATLAB has a notion of packages which can be nested and include both classes and functions.
Just make a directory somewhere on your path with a + as the first character, like +mypkg. Then, if there is a class or function in that directory, it may be referred to as mypkg.mything. You can also import from a package using import mypkg.mysubpkg.*.
The one main gotcha about moving a bunch of functions into a package is that functions and classes do not automatically import the package they live in. This means that if you have a bunch of functions in different m-files that call each other, you may have to spend a while dropping imports in or qualifying function calls. Don't forget to put imports into subfunctions that call out as well. More info:
http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html
I don't see the problem with having to add some folder to Matlab's search path. I have modified startup.m so that it recursively looks for directories in my Matlab startup directory, and adds them to the path (it also runs svn update on everything). This way, if I change the directory structure, Matlab is still going to see all the functions the next time I start it.
Otherwise, you can look into object-oriented code, where you store all the methods in a #objectName folder. However, this may lead to a lot of re-writing code that can be avoided by updating the path (there is even a button add with subfolders if you add the folder to the path from the File menu) and doing a bit of moving code.
EDIT
If you would like to organize your code so that some functions are only visible to the functions that call them directly (and if you don't want to re-write in OOP), you put the calling functions in a directory, and within this directory, you create a subdirectory called private. The functions in there will only be visible to the functions in the parent directory. This is very useful if you have to overload some built-in Matlab functions for a subset of your code.
Another way to organize & reuse code is using matlab's object-oriented features. Each Object is customarily in a folder that begins with an "#" and has the file(s) for that class inside. (though the newer syntax does not require this for a class defined in a single file.) Using private folders inside class folders, matlab even supports private class members. Matlab's new class notation is relatively fully-featured, but even the old syntax is useful.
BTW, my startup.m that examines a well-known location that I do my SVN checkouts into, and adds all of the subfolders onto my path automatically.
The package system is probably the best. I use the class system (#ClassName folder), but I actually write objects. If you're not doing that, it's silly just to write a bunch of static methods. One thing that can be helpful is to put all your matlab code into a folder that isn't on the matlab path. Then you can selectively add just the code you need to the path.
So say you have two projects, stored in "c:\matlabcode\foo" and "c"\matlabcode\bar", that both use common code stored in "c:\matlabcode\common," you might have a function "setupPaths.m" like this:
function setupPaths(projectName)
basedir = fullfile('c:', 'matlabcode');
addpath(genpath(fullfile(basedir, projectName)));
switch (projectName)
case {'foo', 'bar'}
addpath(genpath(fullfile(basedir, 'common')));
end
Of course you could extend this. An obvious extension would be to include a text file in each directory saying what other directories should be added to the path to use the functions in that directory.
Another useful thing if you share code is to set up a "user specific/LabMember" directory structure, where you have different lab members save code they are working on. That way you have access to their code if you need it, but don't get clobbered when they write a function with the same name as one of yours.