MATLAB Answers

How to make switch case in between different classes in matlab?

26 views (last 30 days)
kanuri venkata mohana
kanuri venkata mohana on 8 Jul 2020
Commented: kanuri venkata mohana on 23 Jul 2020 at 18:40
Hi guys,
I have a small question regarding switch case. Iam doing my project using Matlab OOP. Is it possible to make switch case using classes? for example
clMatcore is my super class and clSteel clNano and clIron are my subclasses. I created all the classes and functions for them. Is it possible to make a switch case between them. I was a bit confused in this. Could someone help me out if possible with a small example.
Thankyou

  2 Comments

Steven Lord
Steven Lord on 8 Jul 2020
It's not clear to me what your goal is in using a "switch case using classes". Can you describe (in words not code) what your goal is in selecting between steel, nano, and iron? What problem are you trying to solve?
kanuri venkata mohana
kanuri venkata mohana on 8 Jul 2020
Hi Steven, Thank you for your reply. Here my goal is , i have different materials for my core which are made up of steel, nano, iron. I need to choose my material according to my usage. So i created each class for each material. Now i have to make them in a way, that i can use them according to my design of transformer.
Thankyou..

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 9 Jul 2020
Something like:
switch material
case "steel"
x = clSteel;
case "iron"
x = clIron;
otherwise
error("I don't know how to make a transformer core out of " + material)
end

  5 Comments

Show 2 older comments
kanuri venkata mohana
kanuri venkata mohana on 23 Jul 2020 at 14:34
Hi Steven,
Thankyou for your quick reply. clCore have clmaterialcore as subclass. If the substitution of clmaterialcore object in clCore object is possible even that way could be better.
Because i know to use the properties of parent class in childclass but if i want to do something from child class to parent class then i dont know how to do that.
Thankyou.
Steven Lord
Steven Lord on 23 Jul 2020 at 15:44
Inheritance doesn't seem to be the right approach for some of the classes in your hierarchy.
If I were to plug in a clmaterialcore into a function that expects a clCore, should that function work? If not, clmaterialcore should not be a subclass of clCore.
If I were to plug in a clCore into a function that expects a clmaterialcore, should that function work? If not, clCore should not be a subclass of clmaterialcore.
To me, a mental model where a clCore has a clmaterialcore as one of its properties seems more natural.
classdef clCore
properties
composition % what the core is made of
end
end
Use as:
s = clSteel;
% Build a core.
C = clCore(whateverInputsAreNeededToBuildAclCore);
% Make the core out of steel.
% You probably want to specify this in the constructor call.
C.composition = s;
% Have the core ask its material about the material's conductivity.
C.composition.conductivity
kanuri venkata mohana
kanuri venkata mohana on 23 Jul 2020 at 18:40
Thankyou for your reply Steven i will check with this idea again. I could make switch case with classes as you suggested but, clmaterialcore as input to clCore is not being possible according to my classes. SO i made clmaterialcore as parent class. It worked but it is in reverse order of my Tree diagram.

Sign in to comment.

Tags