2

I have been working on MATLAB scripts. Basically, I have a lot of functions and data files (collectively known as kernels):

I want to organize it a little bit. The idea is

  1. to create a subfolder named functions and save all functions in it.
  2. Another kernels and save all data kernel files in it.

Later by adding these paths at runtime, all the scripts should be able to access these functions and kernels without giving the full path to them, i.e. The script should search it in the subfodlers too.

Applying addpath(genpath(pwd)); worked for functions but it couldn't access kernel files

e.g. What if I want to access file named naif0010.tls inside subfolder kernels.

It didn't work. Any suggestions.

Example:

% Add the current script directory and subfolders to search path
addpath(genpath(pwd));

% Load NASA Spice (mice) to the script here
% add MICE reference path to MATLAB
addpath('C:\Program Files\MATLAB\R2012b\extern\mice\src\mice');
addpath('C:\Program Files\MATLAB\R2012b\extern\mice\lib');

% Load leap second kernel
% If the leapsecond kernel is placed in script directory
% This file is present in pwd/kernel/naif0010.tls
cspice_furnsh('naif0010.tls'); 
Indigo
  • 2,887
  • 11
  • 52
  • 83
  • Have a look to this post, maybe if can help you. [Matlab folder class](http://stackoverflow.com/questions/9781866/folder-and-folder) – Vuwox Nov 18 '13 at 19:55
  • thanks, but nope, still confused – Indigo Nov 18 '13 at 20:03
  • 1
    This depends on how robust the function `cspice_furnsh` is, but you might be able to get away with a relative path like `cspice_furnsh('kernels/naif0010.tls')`, if that helps at all. – nispio Nov 18 '13 at 21:54
  • @nispio: this worked very well after adding pwd earlier. Just updated my answer below. – Indigo Nov 19 '13 at 00:53

2 Answers2

1

There are a couple of things to keep in mind. First, your current working directory (pwd) is in the Matlab path by default, so you don't usually need to explicitly call addpath in order to use scripts, functions, or data files there.

Also, in many cases you can access files by providing a relative path rather than an absolute path. In your case, this would look like

cspice_furnsh('kernels/naif0010.tls')
nispio
  • 1,743
  • 11
  • 22
0

I solved it with some work around which I know is not the correct answer but for now I can go ahead....

addpath(genpath(pwd));
% Basically just forming full path of the data file
leapSecondsFile = fullfile(pwd,'kernels','naif0010.tls');
cspice_furnsh(leapSecondsFile);

Still waiting for correct answer or suggestions :-)

Update:

Thanks nispio's comment above, The correct way is :

% Load current directory and subfolders
addpath(genpath(pwd)); % This is not necessary 
cspice_furnsh('kernels\naif0010.tls');
Indigo
  • 2,887
  • 11
  • 52
  • 83
  • 1
    You can do away with your temporary variable if you want and just call `cspice_furnsh(fullfile(pwd,'kernels','naif0010.tls'));` – nispio Nov 18 '13 at 21:55
  • Indeed, that can be done :-) Although I got the correct answer...updated above – Indigo Nov 19 '13 at 00:55
  • 1
    You probably get away with loading the file without adding `pwd` to your path. By default `pwd` is already in your path. – nispio Nov 19 '13 at 01:00
  • @nispio: Great, that's true, many thanks. In addition, Can you write your suggestion as an answer instead of a comment, so that I can accept it as an answer to this question. Thanks – Indigo Nov 19 '13 at 01:09