Importing class from a different location
Mostra commenti meno recenti
I have a MATLAB class which uses another class defined at a different location. The code is as follows:
classdef Test < handle
properties
CurrentSpeed = 0
SpeedRange = [0, 180]
meIFC2421o meIFC2421
end
methods
function motor = start(motor,speed)
arguments
motor (1,1) Test
speed (1,1) {mustBeReal, mustBeNonnegative}
end
if motor.CurrentSpeed > 0
error("Motor:start:MotorAlreadyRunning",...
"Cannot start a motor that is already running.")
end
motor.CurrentSpeed = speed;
end
function motor = stop(motor)
if motor.CurrentSpeed == 0
error("Motor:start:MotorNotRunning",...
"Cannot stop a motor that is not running.")
end
motor.CurrentSpeed = 0;
end
end
end
On running the code, I get the message that the class meIFC2421 is undefined. The issue gets resolved on running the following code in the command window:
addpath(genpath(pwd))
However, I want to have it somewhere in the code, not in the command window. It should be executed somewhere before the last line of the properties section. Where to place it?
Risposta accettata
Più risposte (1)
Steven Lord
il 20 Giu 2024
The directory containing the meIFC2421 class must be "visible" to MATLAB (either on the MATLAB search path or in the current directory) before MATLAB starts instantiating an instance of the class. There's nowhere inside the class as you've written it that you can add this directory to the path before MATLAB starts to interpret what the properties are.
You could try defining a validation function and using that instead of the class validation functionality you're currently using. Inside that validation function, if the meIFC2421 class is not "visible" to MATLAB, you could add it. Something like this should work, though I haven't tested it.
classdef Test < handle
properties
CurrentSpeed = 0
SpeedRange = [0, 180]
meIFC2421o {mustBe2421Instance(meIFC2421o)}
end
methods
function motor = start(motor,speed)
arguments
motor (1,1) Test
speed (1,1) {mustBeReal, mustBeNonnegative}
end
if motor.CurrentSpeed > 0
error("Motor:start:MotorAlreadyRunning",...
"Cannot start a motor that is already running.")
end
motor.CurrentSpeed = speed;
end
function motor = stop(motor)
if motor.CurrentSpeed == 0
error("Motor:start:MotorNotRunning",...
"Cannot stop a motor that is not running.")
end
motor.CurrentSpeed = 0;
end
end
end
function mustBe2421Instance(value)
if ~exist('meIFC2421', 'class')
% Directory containing this class not visible to MATLAB, add it
addpath(genpath(pwd))
end
assert(isa(value, 'meIFC2421'), 'The value of the meIFC2421o must be a meIFC2421 object.')
% You could check the class() of the value if you don't want to allow
% subclasses of meIFC2421 to be accepted.
end
Though addpath(genpath(pwd)) is a pretty big hammer. You're adding potentially a lot of stuff to the MATLAB search path. Using pwd is a code smell to me; it may not be the directory you think it is if the user has added the directory containing your Test class to the path then used cd to go elsewhere.
If you want to add a directory relative to the directory containing your Test class, you could use the which and mfilename (with the 'class' input argument) functions to determine where Test lives and add a path relative to that location (constructed using fileparts and fullfile) to the path.
Categorie
Scopri di più su Search Path in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!