1

I have MATLAB R2014b. As you know when we code in other programming environments like visual studio we have a online compiler but in MATLAB after editing a code (object oriented code) we should use clear classes before running it with new structure. Why we have this structure in MATLAB? Why we don't have a environment like visual studio!?

Eghbal
  • 3,892
  • 13
  • 51
  • 112
  • 2
    Since you appear to be doing OOP, [`clear classes`](http://www.mathworks.com/help/matlab/ref/clear.html#bt2itb8)? While valid, I'm afraid that your question might not be specific enough. It would help if you provided a small example with step by step instructions of what you're doing in order for others to try to replicate your issue. Does the down voter care to comment on why they think that this is a bad question? – horchler Jan 08 '15 at 19:32
  • 1
    What do you mean by "online compiler"? I'm not that familiar with Visual Studio. – TroyHaskin Jan 08 '15 at 22:01
  • @horchler Thank you for your comment. I edited the question. I have a question : Why every time we should use `clear classes`. It is very tedious to use `clear classes` after every change in codes! This is only way to deal with this situation? – Eghbal Jan 08 '15 at 22:02
  • @TroyHaskin Thank you for comment. I don't know the correct name of it :-). As you know in environments like visual studio when we are coding simultaneously it will compile and we don't need anything like `clear classes`after editing classes. – Eghbal Jan 08 '15 at 22:04
  • Short answer: Because otherwise Matlab doesn't work. It's an interpreter anyway. And that may just be the way it works. The real question is probably: "Are there any advantages of having to clear every time?". – NoDataDumpNoContribution Jan 08 '15 at 22:21
  • Actually I think the Matlab support is the one who can really answer this question if anyone. – NoDataDumpNoContribution Jan 08 '15 at 22:23

2 Answers2

2

Updating Classes

It is a little annoying to have to repeatedly clear classes; to have MATLAB not complain about the definition having changed. Luckily, with R2014b, the MathWorks has taken a large step forward in remedying this annoyance by implementing Automatic Updates for Modified Classes, which has a lot fewer conditions for a full memory flush than earlier versions (R2014a and prior).

The reason for why this ability didn't exist from the beginning is that MATLAB's current object model [PDF] was deployed only seven years ago. For the lifetime of MATLAB, this is a fairly new feature with a lot of improvements than can be and, probably, will be made (like the automatic updating).


Environment like Visual Studio

Visual Studio is a program that aids in the development of code and their compilation. Everytime a code base is compiled, the behavior of the classes is parsed and the former definition is overwritten (note that the behavior of the class need not be fixed at compile time). When the compiled program or library is used, those objects are then instantiated and used.

In MATLAB, when you instantiate an object, the definition from the classdef is parsed, and an instance is loaded into memory. If the classdef is changed and another instantiation is invoked, older versions of MATLAB would complain since the MATLAB runtime "allows only one definition for a class to exist at any time" (docs). The latest release, however, performs an in-place update for all instances currently loaded into memory, save a few exceptions.

I think the two use cases show the difference in behavior: the compiled program only ever has one class definition to deal with and the only way to change the signature is to re-compile the code. MATLAB, however, can have the class signature change while having an instance of that class already in memory, and MATLAB's stance is one that says, quite rightly in my opinion, only one class definition shall exist at one time.

Community
  • 1
  • 1
TroyHaskin
  • 8,361
  • 3
  • 22
  • 22
0

You need to do clear classes if you have any objects of the class you are editing in your workspace. Once you create an object of class Foo, the implementation of Foo gets cached. Otherwise, if you change the code of Foo while you have an object of type Foo in your workspace, that object may become inconsistent.

Also, MATLAB uses an interpreter, not a compiler.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • 1
    Matlab uses a compiler as well to accomplish JIT compilation. It might be more correct to say that "Matlab is an interpreted language" rather complied one (as suggested [here](http://en.wikipedia.org/wiki/Interpreter_(computing))). The two are not mutually exclusive. – horchler Jan 09 '15 at 01:28