Octave adding path of folder and subfolder

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Octave adding path of folder and subfolder

usman


I have a main program that calls functions to perform a task. However, these
functions are in another directory of the main folder (or these functions
are in sub-directories). I am not sure which commands may I include to
perform this task without any errors.

I tried using addpath(), load(), etc. but I don't know the proper usage, may
be that's a reason.

Maincode.m (Is a file in directory e.g /program/example/maincode.m):

% start of code
plot_traction_force(t_cycle,F_trac_veh);
%end of code

plot_traction_force.m (Is a file in directory e.g
/program/plotfunctions/plot_traction_force.m):

% start of code
function [] = plot_traction_force(t_cycle,F_trac_veh)
% code goes here
end
%end of code

The directory view:

|-->Program
 |-->example
    |--> maincode.m
 |-->plotfunction
    |--> plotftn1.m
    |--> plotftn2.m
 |-->extension
    |-->vehicle_data.m  
    |-->electric_machine_map.m

where plotftn1.m, vehicle_data.m, electric_machine_map.m are functions
called by maincode.m.

I don't know how to implement this for a number of functions that might be
in different directories of the parent directory.

I want to provide my program as a zip file and when the user has it , it
executes and every .m file can call each other in the folder
<http://octave.1599824.n4.nabble.com/file/t373187/2019-02-04.png>



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Octave adding path of folder and subfolder

apjanke-floss

On 2/4/19 3:48 PM, usman wrote:

>
>
> I have a main program that calls functions to perform a task. However, these
> functions are in another directory of the main folder (or these functions
> are in sub-directories). I am not sure which commands may I include to
> perform this task without any errors.
>
> I tried using addpath(), load(), etc. but I don't know the proper usage, may
> be that's a reason.
>
> Maincode.m (Is a file in directory e.g /program/example/maincode.m):
>
> % start of code
> plot_traction_force(t_cycle,F_trac_veh);
> %end of code
>
> plot_traction_force.m (Is a file in directory e.g
> /program/plotfunctions/plot_traction_force.m):
>
> % start of code
> function [] = plot_traction_force(t_cycle,F_trac_veh)
> % code goes here
> end
> %end of code
>
> The directory view:
>
> |-->Program
>   |-->example
>      |--> maincode.m
>   |-->plotfunction
>      |--> plotftn1.m
>      |--> plotftn2.m
>   |-->extension
>      |-->vehicle_data.m
>      |-->electric_machine_map.m
>
> where plotftn1.m, vehicle_data.m, electric_machine_map.m are functions
> called by maincode.m.
>
> I don't know how to implement this for a number of functions that might be
> in different directories of the parent directory.
>
> I want to provide my program as a zip file and when the user has it , it
> executes and every .m file can call each other in the folder
> <http://octave.1599824.n4.nabble.com/file/t373187/2019-02-04.png>
>
>
>
> --
> Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
>
>


Hi, usman,

You need to call addpath() using the full path to each directory which
contains your M-code files. This can be done in separate calls to
addpath(), or one call to addpath() that passes each directory as a
separate argument (or even as one long pathsep-separated string).

In my experience, in a project like this, it's easiest to do this in a
single "init_<project>.m" file at the root of the project that you call
once in each session when loading your project. Have it use
`fileparts(mfilename('fullpath'))` to detect where it's being run from,
and then call fullfile() on that to construct the full path to the
source directories in your project. In your case, this would look
something like:

% file init_EVD_Program.m
function init_EVD_Program
this_file = mfilename('fullpath');
dist_dir = fileparts(this_file);
src_subdirs = {
   'Functions/DataSheetFunctions'
   'Functions/PlutFunctions'
   'InputData/ElectricMachineMap'
   'InputData/EMachineLOSSParameters'
   'InputData/Extensions'
   'InputData/VehicleParameters'
   'InputData/WLTP'
   'InputData'
};
for i = 1:numel(src_subdirs)
   src_dir = fullfile(dist_dir, src_subdirs{i});
   addpath(src_dir);
endfor

fprintf('Loaded EVD_Program from %s\n', dist_dir);
endfunction

Then, when starting a new Octave session to work with this project,
you'd cd to its directory and call init_EVD_Program from the Octave
command line. That'll load up all your paths, and then you can call your
code, and that code can call any other code in your project.

Instead of enumerating all the source subdirectories, you could also
just use dir() to recurse through the subdirectories of your program
distribution, and choose to add all of them that contain any *.m or
*.mat files.

 From what I've seen, it's Octave convention to just stick all the *.m
files for a project in a single inst/ or code/ subdirectory, and not
break them out into topic areas like this, so there's no tool to do this
for you in a concise manner. But IMHO that's no reason not to do it;
breaking out code like this can be useful for conceptual organization.

Matlab provides a "Set Path" GUI tool for adding your folders to the
Matlab path. Using this tool permanently modifies the Matlab path on an
installation-wide basis. Octave omits this tool; I assume because it was
a terrible design decision on Matlab's part that they wanted to avoid
replicating. My advice is to never use Matlab's Set Path tool.

If you want to get advanced with your distribution, you could structure
your program as an Octave package, which provides additional metadata
and installation support for users of your program.
https://octave.org/doc/v4.4.1/Creating-Packages.html#Creating-Packages.
AFAIK, it doesn't have multi-directory path management, though, so you'd
still have to provide something like init_EVD_Program.m.

Cheers,
Andrew