adding search path before classdef of derived class

19 次查看(过去 30 天)
% 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.

采纳的回答

Steven Lord
Steven Lord 2022-5-7
Your class myBase written in the file base.m is not known to MATLAB as myBase. When the name of the class or main function in a file differs from the file name, MATLAB knows it by the file name. You have two main options:
  1. Put base.m on your path and have your derived class inherit from base instead of myBase.
  2. Change the filename of base.m to myBase.m and put myBase.m on your path.
Another possible option, if you don't want your base class to be in the global namespace, would be to put it in a package directory. If myBase.m were in a folder named +myclasses then you would inherit from myclasses.myBase.
The base class for a derived class must be accessible to MATLAB at the time you instantiate the derived class instance. There's no time to make it accessible (via path manipulation or the like) after you've told MATLAB to start constructing the class but before it actually starts.
  1 个评论
Florian Berzsenyi
Sadly, this was not it. I made a mistake when writing the example code snippet. My original code works fine when I put both classes in the same directory. I just wanted to store a set of base classes in a central location, instead of having to store multiple copies in every location where the base classes are needed.
If I understand you correctly, this is not possible because MATLAB resolves the class declaration before runtine. The base class is not instantiated before the derived class and thus cannot be found by MATLAB when it looks for the base during instantiation of the derived class.
I tried to work around this issue by using a local pathdef.m with the path of base.m added, and startup.m that calls addpath(). The former does not work (see OP), and the latter does solve the issue BUT only if MATLAB is started with the working directory set to the path of derived.m.
What I tried to look for is nothing more than a #include <.m> variety in MATLAB. It just does not exist. If I were with the MATLAB devs, I would add a path parameter to the class attributes so one could write:
% HYPOTHETICAL class attribute: On runtime, before instantiation,
% MATLAB reads the attribute path, and calls addpath() with the provided value.
classdef base (path = 'D:\notPwd') < handle
methods
% C'tor etc.
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Class File Organization 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by