[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Octave adding path of folder and subfolder

From: Andrew Janke
Subject: Re: Octave adding path of folder and subfolder
Date: Mon, 4 Feb 2019 16:22:01 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Thunderbird/60.4.0

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
%end of code

plot_traction_force.m (Is a file in directory e.g

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

The directory view:

     |--> maincode.m
     |--> plotftn1.m
     |--> plotftn2.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

Sent from:

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 = {
for i = 1:numel(src_subdirs)
  src_dir = fullfile(dist_dir, src_subdirs{i});

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

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. AFAIK, it doesn't have multi-directory path management, though, so you'd still have to provide something like init_EVD_Program.m.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]