Matlab Structure Fake Constructor – Mr. Joel Kemp

Matlab structures are awesome! However, the dynamically typed and function-based nature of the language makes some object-oriented fundamentals a bit unintuitive (especially with a background in other OOP languages: C++, Java, C#, Python, etc). Here’s a workaround that simulates structure constructors and makes OOP in Matlab feel a bit more familiar.

UPDATE: Matlab class definitions support a similar style of constructors. However, classes require a special directory structure; that’s a bit more foreign than I’m willing to handle.

Structures are created within functions, which means, that if two functions want to create an instance of some structure, they need separate definitions.

Example: Let’s create a structure that represents a Student

function foo()    %Student structure defined within foo()    s = struct("id", [], "name", [], "age", [], "email", []);  end

function foo() %Student structure defined within foo() s = struct(“id”, [], “name”, [], “age”, [], “email”, []); end

Notice, of course, that there was no identifier/label “Student” associated with this definition. Quite simply, there’s no explicit identification in the syntax. But what if bar() needs to declare an instance of that struct? The struct will have to be defined there as well.

function bar()    s = struct("id", [], "name", [], "age", [], "email", []);end

function bar() s = struct(“id”, [], “name”, [], “age”, [], “email”, []); end

Of course, the reason for this necessary structure redeclaration is that Matlab doesn’t support type definitions (think C Struct or C++ class definitions). It would be amazing if you could just call s = Student();, but that’s not the case.

Although, we could force the case by creating a separate M-file containing the definition for such a function Student()! That function would contain the aforementioned structure definition and return an instance of that structure!

In Student.m

function S = Student()   S = struct("id", [], "name", [], "age", [], "email", []);end

function S = Student() S = struct(“id”, [], “name”, [], “age”, [], “email”, []); end

This may seem like a trivial function, but it’s really awesome on many levels! Let me explain.

Firstly, the definition for the Student structure is contained within this one file (FYI: putting the function in a separate file makes it global in scope to every other file in your workspace directory!) so that modifications to the definition are isolated to this one M-File.

Secondly, the desired sytax is now a reality! We can call s = Student() from any file/function in our project and that variable will contain the structure for a Student.

Thirdly, let’s say you want to simulate a custom constructor that takes in a set of parameters and sets the values of the Student structure’s attributes, then you can easily do so by modifying the Student() function to accept arguments and determining how many arguments were passed using Matlab’s varargin keyword! If the number of variable arguments passed is 0, then your constructor should behave like a default constructor!

I’m pretty sold on this idea!