MATLAB Answers

How do I add a class to a class

2 views (last 30 days)
Alok Virkar
Alok Virkar on 23 Jun 2020
Answered: Robert U on 25 Jun 2020
I created this class to initialize properties of a specific rocket stage:
classdef stageClass < handle
properties
stageNum
isp
mtot
mprop
mstructure
mpayload
end
methods
function obj = stageClass(stageNum)
obj.stageNum = stageNum;
end
end
end
classdef rocket < handle
%This class will create an object called rocket.
%This rocket will have stages with properties
properties
stage = stageClass.empty;
end
methods(Static)
function obj = rocket()
end
function obj = addStage(obj, stageNum)
obj.stage(length(obj.stage)+1) = stageClass(stageNum);
end
end
end
I have another class called rocket from which I want to intialize a variable/ user input amount of stages. My goal here is to create a class that can initialize stages. For example I want the input to be: rocket.stages = 3. Then it would create 3 instances of the stages, each with unique properties which I could initialize individually.
How do I write a function that will add a stage to the rocket?

Answers (2)

Robert U
Robert U on 25 Jun 2020
Hi Alok Virkar,
There are several possibilities to fullfill the described functionality. But first there are issues with your class "rocket". Neither constructor method nor any method that changes properties may be "static". Static methods are well suited for value calculations or data operations, i.e. functions that do not manipulate the object nor depend on changing object properties.
My suggestion for adding rocket stages is to use a customized set-method for the property "stage" of class "rocket". I change the data type of "stage" to cell which makes it easy to support several stages.
classdef rocket < handle
%This class will create an object called rocket.
%This rocket will have stages with properties
properties
stage = {}; % could be a cell array Nx1 containing stageClass objects
end
methods
function set.stage(obj,cStageClass)
assert(all(cellfun(@(cIn) isa(cIn,'stageClass'),cStageClass)),'The input cell does not contain objects of class ''stageClass''.');
if isempty(obj.stage)
obj.stage(1) = cStageClass;
fprintf(1,'<< Stage No. %d added.\n',cStageClass{1}.stageNum);
else
dExistingStageNums = cellfun(@(cIn) cIn.stageNum,obj.stage);
if any(dExistingStageNums == cStageClass{1}.stageNum)
warning('Stage No. %d exists already. It will not be added.',cStageClass{1}.stageNum);
else
% append new stage
obj.stage(end+1) = cStageClass;
fprintf(1,'<< Stage No. %d added.\n',cStageClass{1}.stageNum);
% sort according to stageNum
dExistingStageNums = cellfun(@(cIn) cIn.stageNum,obj.stage);
[~,indSort] = sort(dExistingStageNums);
obj.stage = obj.stage(indSort);
end
end
end
end
methods
function obj = rocket()
end
function addStage(obj,stageNum)
validateattributes(stageNum,{'numeric'},{'vector','integer','positive'});
for nStage = stageNum
obj.stage = {stageClass(nStage)};
end
end
end
end
The set-method controls whether a new stage is added or not to avoid duplicate numbers, and sorts the cells according to there stage number. Furthermore, I added exemplary input argument checks which will help you to create more robust code.
In difference to your description, each stage has to created by supplying its particular number. Call the following way:
myRocket = rocket; % create object of class "rocket"
myRocket.addStage(1:3); % create stages 1, 2, and 3
myRocket.addStage(5:-1:3); % create stages 4 and 5 (inverse order to check sorting, withdrawing 3 since it already exists)
myRocket.addStage([7,11,14]); % create stages 7,11,14 (arbitrary numbers)
myRocket.addStage(9);
Kind regards,
Robert

Robert U
Robert U on 25 Jun 2020
Hi Alok Virkar,
another approch making use of object arrays which might be more convenient in use:
rocket.m
classdef rocket < handle
%This class will create an object called rocket.
%This rocket will have stages with properties
properties
stage = stageClass.empty; % object array 1xN of stageClass objects
end
methods
function set.stage(obj,StageClass)
assert(isa(StageClass,'stageClass'),'The input cell does not contain objects of class ''stageClass''.');
for nStage = 1:numel(StageClass)
dExistingStageNums = [obj.stage.stageNum];
if any(dExistingStageNums == StageClass(nStage).stageNum)
warning('Stage No. %d exists already. It will not be added.',StageClass(nStage).stageNum);
else
% append new stage
obj.stage(end+1) = StageClass(nStage);
fprintf(1,'<< Stage No. %d added.\n',StageClass(nStage).stageNum);
% sort according to stageNum
dExistingStageNums = [obj.stage.stageNum];
[~,indSort] = sort(dExistingStageNums);
obj.stage = obj.stage(indSort);
end
end
end
end
methods
function obj = rocket()
end
function addStage(obj,stageNum)
validateattributes(stageNum,{'numeric'},{'vector','integer','positive'});
obj.stage = stageClass(stageNum);
end
end
end
stageClass.m
classdef stageClass < handle
properties
stageNum
isp
mtot
mprop
mstructure
mpayload
end
methods
function obj = stageClass(dStageNum)
if nargin ~= 0
for indStage = 1:numel(dStageNum)
obj(indStage).stageNum = dStageNum(indStage);
end
end
end
end
end
Test sequence:
myRocket = rocket; % create object of class "rocket"
myRocket.addStage(1:3); % create stages 1, 2, and 3
myRocket.addStage(5:-1:3); % create stages 4 and 5 (inverse order to check sorting, withdrawing 3 since it already exists)
myRocket.addStage([7,11,14]); % create stages 7,11,14 (arbitrary numbers)
myRocket.addStage(9);
Kind regards,
Robert

Tags

Community Treasure Hunt

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

Start Hunting!