One of the object oriented programming (OOP) features in MATLAB is the ability to define classes, which can be used to facilitate encapsulation of data and operations. Classes are defined in MATLAB within a `classdef` block, followed by `properties`, `methods`, `events` and `enumeration` subblocks.
Questions tagged [matlab-class]
103 questions
152
votes
4 answers
Is MATLAB OOP slow or am I doing something wrong?
I'm experimenting with MATLAB OOP, as a start I mimicked my C++'s Logger classes and I'm putting all my string helper functions in a String class, thinking it would be great to be able to do things like a + b, a == b, a.find( b ) instead
of strcat(…

stijn
- 34,664
- 13
- 111
- 163
44
votes
10 answers
How do I create enumerated types in MATLAB?
Are there enumerated types in MATLAB? If not, what are the alternatives?

iddober
- 1,254
- 4
- 23
- 43
42
votes
8 answers
Constants in MATLAB
I've come into ownership of a bunch of MATLAB code and have noticed a bunch of "magic numbers" scattered about the code. Typically, I like to make those constants in languages like C, Ruby, PHP, etc. When Googling this problem, I found that the…

Benjamin Oakes
- 12,262
- 12
- 65
- 83
32
votes
3 answers
How do properties work in Object Oriented MATLAB?
I am trying to create a MATLAB class with a member variable that's being updated as a result of a method invocation, but when I try to change the property within the class it (apperently, from what I understood from MATLAB's memory management)…
Dani
26
votes
2 answers
How to modify properties of a Matlab Object
I've created a MATLAB class, something like:
classdef myclass
properties
x_array = [];
end
methods
function increment(obj,value)
obj.x_array = [obj.x_array ; value);
end
end
end
The problem is, the property x_array is…

user34830
- 261
- 1
- 3
- 3
18
votes
4 answers
Best way to organize MATLAB classes?
MATLAB has two ways of organizing classes:
@-directories:
@ClassName\
ClassName.m
Method1.m
Method2.m
Single files:
ClassName.m:
classdef ClassName
methods
% all methods included here
end
end
The first style existed…

jjkparker
- 27,597
- 6
- 28
- 29
16
votes
4 answers
How to obtain static member variables in MATLAB classes?
Is there a way to define static member variables in MATLAB classes?
This doesn't work:
classdef A
properties ( Static )
m = 0;
end
end
It suggests to use keyword "Constant" instead of "Static", the constant properties cannot be…

Vahagn
- 4,670
- 9
- 43
- 72
15
votes
2 answers
How can I tell how much memory a handle object uses in matlab
If I declare an object to be a subclass of handle
classdef obj < handle
my object is now essentially a "pointer" to some memory somewhere. How do I find out how much memory my object is using up?
For example, say I have a class foo with a field…

Marc
- 5,315
- 5
- 30
- 36
15
votes
1 answer
Matlab segmentation fault when iterating vector assignment
I've been vectorizing some matlab code I'd previously written, and during this process matlab started crashing due to segmentation faults. I narrowed the problem down to a single type of computation: assigning to multiple struct properties.
For…

zergylord
- 4,368
- 5
- 38
- 60
14
votes
2 answers
How do I create an array of abstract class objects in MATLAB?
As an example, suppose I have created an abstract class called Shape and two subclasses called Circle and Rectangle that both implement an (abstract) method called Draw. I would like to be able to create a number of Circle and Rectangle objects,…

user2978125
- 342
- 2
- 9
14
votes
1 answer
indexed object dot notation method gives scalar property
I'm seeing an issue when I try and reference an object property after having used a dot notation to apply a method.
it only occurs when I try to index the initial object
classdef myclassexample
properties
data
end
methods
function…

dylan2106
- 402
- 1
- 3
- 12
11
votes
1 answer
MATLAB - run object destructor when using 'clear'?
Suppose I have a class myClass < handle. From the Mathworks Help page on clear,
Clearing handle graphics handles does not remove the objects themselves, nor does deleting the objects remove variables storing their handles.
hf = figure; %…

Dang Khoa
- 5,693
- 8
- 51
- 80
10
votes
1 answer
How do I access the new value of an PropertyEvent in MATLAB R2014b?
Background
In previous versions of MATLAB (e.g. R2013b), I had a neat trick where I would attach a listener to an axes handle's YLim property, which would notify me when the axes y-limits were changed:
addlistener(gca, 'YLim', 'PreSet',…

KQS
- 1,547
- 10
- 21
10
votes
2 answers
Matlab Polymorphism
I've got two new-style MATLAB classes - B & C, both concrete subclasses of an abstract parent, A. A is a subclass of hgsetset (handle class). I'd like to put them in an array in MATLAB, and treat them both as As. They are defined, roughly,…

Marc
- 3,259
- 4
- 30
- 41
9
votes
3 answers
MATLAB - create reference (handle?) to variable
Suppose I have the following class:
classdef myClass < handle
properties
A = 1
end
methods
function obj = myClass(val)
obj.A = val;
end
end
end
Say I instantiate an instance of this class, then…

Dang Khoa
- 5,693
- 8
- 51
- 80