- Put base.m on your path and have your derived class inherit from base instead of myBase.
- Change the filename of base.m to myBase.m and put myBase.m on your path.
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.
0 件のコメント
採用された回答
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:
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Class File Organization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!