0

I have an issue, where I need to handle a lot of figures in matlab and the code is starting to get messy. Different kinds of plot objects are added to the code in different stages and some have legends and some does not. The problem is that there is no NULL legends. As soon as an object is created, so is a legend. However, until the legend(handles,...) is called they are not shown. This means that if things are plotted and some, need a legend entry and some not, a lot of handles needs to be passed around.

Now, the file is starting to be quite long, about 1500 lines, with some globals that spans over many functions in the file and so. To prevent the "Do not use globals" comments to pour in, yes I know globals are normally unnecessary, but the code was like that when I laid my hands on it. However, now the code is getting more and more messy and I think about using Object Oriented Programming (OOP) to handle figures.

The idea is to have the custom figure objects handling themselves and thus make more readable code, split up in smaller blocks. The idea is to have a design like

class Figure

private:

MainFrame;
SubFrame;
Lines;
Legends;
Title;
X-Label;
Y-Label;

Methods:

To be defined, for example formatting plotting, edit title,… 

The complete design is not really thought through completely, but the point of this questions is really about using OOP in matlab. What I have seen so far it os not really used were much. Are there a reason for this? Could anyone give pros and cons to OOP in matlab? Is OOP recommended or not in matlab?

I have added the information about my issue since I understand that OOP is more needed for large complex issues, so an answer would preferably take the drawbacks in comparison with the complexity of the problem into account. (For example, do never use OOP in matlab, do it only when you have complex problems, do it whenever you like,...)

patrik
  • 4,506
  • 6
  • 24
  • 48
  • 3
    [Matlab documentation](http://www.mathworks.com/help/matlab/matlab_oop/why-use-object-oriented-design.html?refresh=true) has a fairly large page on this very topic, and guidelines on when to ue OOP. Also pros and cons to OOP is not really dependant on the language (unless the language doesn't have enough features to support OOP). It depends on the problem you are solving (complexity, modularity requirement etc). – Rollen Apr 02 '15 at 13:01
  • 1
    @RollenD'Souza I see, but that is more or less true for any object oriented language and it is extremely high level. This question is not about OOP, but OOP in MATLAB. The question is loosely related to this older SO question [Is MATLAB OOP slow or am I doing something wrong?](http://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong?rq=1). However, the question is quite old and may not be representive any more. – patrik Apr 02 '15 at 13:05
  • Ahh i see. Okay, that makes more sense. The accepted answer has been updated for 2014a as well though so it is representative. – Rollen Apr 02 '15 at 13:18
  • If you look at the first answer, he re-ran the code on MATLAB 2014a and OOP was still much slower than direct function calls, as were packages. – TheBlackCat Apr 02 '15 at 13:23
  • @TheBlackCat that is true, but on the other hand this is just function call. There may be other issues as well, which is not shown here. – patrik Apr 02 '15 at 13:56
  • @patrik Yes, how big of an issue the functional call overhead is depends on how many function calls you are doing. If there is a lot of cross-talk within or between objects, it could be a big problem. If there isn't, then it probably won't matter that much. – TheBlackCat Apr 03 '15 at 07:33

1 Answers1

1

Okay the question is about OOP in Matlab - but is it not OOP in Matlab in your organisation?

By that I mean to think who is going to use/develop and maintain the code going forward.

Background: I have used OOP for my own toolbox (because its complex/large enough to warrant it - and I develop/maintain it) - however in consultancy jobs for the majority of my clients I create functions (which in some instances call my toolbox) - because when the job is finished they get the source code and the majority are (much) more comfortable working with functions rather than classes.

In summary - I decide on whether to use OOP on the job specifics and the situation where the code will be used (developed & maintained) in the future.

So back to your topic - I would consider where you think the code is going to go and who will develop/maintain it. Will they be comfortable with classes - or will they be more comfortable with functions?

FYI: Last year I was talking to Mathworks and they said that they run multiple "Intro to Matlab" courses per week - but only 1 "Matlab Classes" per quarter!! That gives you an indication on the level of Matlab class use in industry.

matlabgui
  • 5,642
  • 1
  • 12
  • 15