adding search path before classdef of derived class
Mostrar comentarios más antiguos
% EDIT: Corrected mismatching .m-filename and class name in the example. My bad!
I would like to construct a derived class derived.m that can be launched directly by pressing the "Run"-scipt button for example. However, the base class base.m is located in a path, which is unknown to MATLAB at runtime.
%file: pwd\derived.m
classdef derived < base
methods
function obj = derived()
%call superclass (base class)
obj = obj@base;
end
end
end
% file: D:\notPwd\base.m
classdef base < handle
methods
function obj = base()
disp("Base called");
end
end
end
Normally, I would add the file base.m to a subfolder +base in the directory of derived.m, or call addpath() at the beginning of the script. Unfortunately, I cannot do the latter with a class because no code is allowed to be inserted before the keyword classdef.
addpath('D:\notPwd'); % % % NOT ALLOWED, derived.m is a class defintion file, not a script !
classdef derived < base
% [...]
Another solution (at least I thought it would be): creating a local copy of pathdef.m with the path to base.m added. On the downside, I would have to create pathdef.m manually in the console or with a helper script but once it is created, MATLAB would load the local file over the default one in the matlab directory.
addpath('D:\notPwd'); % step 1: add path of base.m to search paths of the current open session
savepath(fullfile(pwd, 'pathdef.m')); %step 2: save search paths to pathdef.m located in current working directory
% % % REOPEN MATLAB % % %
myDerived.m; % step 3: run myBase.m. MATLAB uses the local pathdef.m, locates myBase.m from added search path ("D:\...")
Unfortunately, this does not work either, although MATLAB seems to select the correct file (i.e. the local pathdef.m) on startup. I checked that by calling which pathdef in the console. And yes, the specified search path is correct. I did a test run of myDerived.m after calling addpath() and befored calling savepath(). It worked fine.
Why does the above does not work then? Are class definitions interpreted before the search paths are loaded (declaration > definition) ? Are there other alternatives? I have looked at the class attributes in the hopes of finding an attribute path that would allow me to specifiy a search path on definition (e.g. classdef(path = 'D:\notPwd') but found nothing.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Search Path en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!