Importing class from a different location

23 vues (au cours des 30 derniers jours)
Syed Ali Haidar
Syed Ali Haidar le 20 Juin 2024
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?

Réponse acceptée

Pavan Sahith
Pavan Sahith le 20 Juin 2024
Hello Syed Ali,
It seems like you're aiming to seamlessly integrate the addpath command within your class definition to ensure that all necessary paths, especially those related to the meIFC2421 class, are automatically included.
To achieve that , you can define a static method within your class to set up the path. You can then call this method at the beginning of your script or main function. However, note that this method needs to be called explicitly before instantiating the class.
methods(Static)
function setupPath()
addpath(genpath('path_to_your_directory_containing_meIFC2421'));
end
end
To utilize the Test class and ensure all paths are correctly set up, you would first call this method like so:
Test.setupPath();
motor = Test();
Alternatively, for a more automated and global approach, leveraging a startup.m file could be an excellent strategy. This file is automatically executed by MATLAB at launch, allowing you to set up any necessary paths right from the get-go. you can follow these steps
  1. Create a 'startup.m' file: This file should be placed in a directory that is on your MATLAB path. MATLAB executes this script automatically at startup.
  2. Add the addpath command: Include the necessary addpath commands in this file to ensure the required directories are added to the MATLAB path.
% startup.m
addpath(genpath('path_to_your_directory_containing_meIFC2421'));
For more information, you can refer to the following MathWorks documentation
consider referring to this MATLAB answer which might help you
Hope this helps you moving forward

Plus de réponses (1)

Steven Lord
Steven Lord le 20 Juin 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.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by