MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Volkan Kandemir
on 16 May 2013

Hello, I am trying to learn classes in Matlab here is a basic question. How can I write two classes A and B in order to represent following structur?

My first class is A and second class is B. Let A has two property as p and q and B has also two property as m and n. Moreover, one of class is subclasses of other.

A.p=1; A.q=2;

B.m=3; B.n=4;

A.B=B; %Minor question :) which one is a subclass A or B?

%Hence

A.B.m %should be 3 A.B.n %should be 4

Answer by per isakson
on 16 May 2013

Edited by per isakson
on 16 May 2013

Accepted answer

It is a convention to use upper case for the first letter in a class name and lower case for instancies/objects

Try

>> a = A(); >> a.b.m ans = 3

where

classdef A properties p = 1; q = 2; b = B; end end

and

classdef B properties m = 3; n = 4; end end

.

One more example

>> A.B.m ans = 3

where

classdef A properties ( Constant ) p = 1; q = 2; B = B(); end end

and

classdef B properties ( Constant ) m = 3; n = 4; end end

However, I don't think this is what you really ask for.

Volkan Kandemir
on 17 May 2013

Thank you very much for your answer. That is what I excatly want. Also thank you for correcting me about upper and lower cases. Where can I find these rules. For example, all cracters in global variables should be writen as UPPER CASE like.

global WORKERS

per isakson
on 17 May 2013

There is very little on naming conventions in the Matlab documentation.

google for *"object oriented programming naming conventions"*

See the blog Book Review: The Elements of MATLAB Style

## 3 Comments

## per isakson

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76061#comment_149358

Search for "Defining a Subclass" in the help. As far I can judge, you describe an association.

## Volkan Kandemir

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76061#comment_149365

I have read lots of articles and I have writen some classes but I can not combine classes. When I try to make one of them subclases I get the following resault.

## per isakson

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76061#comment_149378

You have defined A as a subclass of B and the result you see is as documented, i.e. what you should expect.