0

I am having some issues with matlab now that I have added a new variable to a class. I have tried using clear classes as the error message says to but this seems to still produce the same message. I have also tried every clear command possible, rehash commands, restarting matlab and am now completely out of ideas..

Don't suppose anyone else has any? I'm using matlab 2013b if that's any use!

Cheers!

braX
  • 11,506
  • 5
  • 20
  • 33
bdavies6086
  • 382
  • 1
  • 5
  • 19
  • @RobertP. Hi! Here's the message I'm getting.. Cannot change the number of fields of class 'netcdf' without first typing 'clear classes'. Cheers! – bdavies6086 Sep 23 '14 at 09:56
  • @bdavies6086: are you changing the exiting NetCDF functions that ship with MATLAB (`$MATLABROOT\toolbox\matlab\imagesci\+netcdf\*.m`)? If not try restarting MATLAB (but dont create any instances of your class juet yet), make the changes you want (adding fields), then create objects from the new class. – Amro Sep 23 '14 at 11:04
  • @Amro Hi! Thanks for the suggestion, no the modifications are not for the NETCDF functions shipped with matlab, it's for an api which interacts with these functions. I have tried your suggestion and the error has still occurred, thanks anyway though! :) – bdavies6086 Sep 23 '14 at 12:13

1 Answers1

0

Since you're not providing much info, let me show what I get with a simple example running on R2014a. Consider the following class:

MyClass.m

classdef MyClass
    properties
        x
    end
end

First I create an object of this class:

>> a = MyClass()
a = 
  MyClass with properties:

    x: []

Next I modify the above by adding a new property y, and saving the MyClass.m file:

MyClass.m (after change)

classdef MyClass
    properties
        x
        y     %# <-- added
    end
end

Now if I try to create another instance of the class, I get the following warning:

>> b = MyClass()
Warning: The class file for 'MyClass' has been changed, but the change cannot be applied because objects
based on the old class file still exist. If you use those objects, you might get unexpected results. You
can use the 'clear' command to remove those objects. See 'help clear' for information on how to remove
those objects. 
b = 
  MyClass with properties:

    x: []

As indicated, we won't see the changes until we clear all instances of the old class:

>> whos
  Name      Size            Bytes  Class      Attributes

  a         1x1               104  MyClass              
  b         1x1               104  MyClass              

>> clear classes

>> whos

>> b = MyClass()
b = 
  MyClass with properties:

    x: []
    y: []

Now the modifications are picked up by the new object.

Note that in some cases, MATLAB might still hold references to objects not visible in the base workspace (like if you have globals, locked functions, or GUIs with data saved in the app/user data section of the GUI). Usually closing all figures and clearing all variables will remedy the situation. If not, restarting MATLAB will most definitely get could back to a clean slate.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • 1
    As well as the list of "hidden" places @Amro suggests such as locked functions and app/user data of GUIs, check for objects hiding in the workspace of function handles, particularly those in `timer` object callbacks that can't be accessed from the base workspace. I often get bitten by that. – Sam Roberts Sep 23 '14 at 14:24
  • thanks I forgot about function handles, especially those that create closures. Like you said, timers can create [difficult cases](http://stackoverflow.com/q/10489996/97160). As a last resort, a MATLAB restart will get rid of all these things, which is obvious since the process is terminated and its entire memory reclaimed by the OS.. For a less drastic solution, [there are](http://www.mathworks.com/matlabcentral/fileexchange/47609-nuke) [ways](http://www.mathworks.com/matlabcentral/answers/1093-how-do-i-reset-matlab-to-its-launched-state) to clear all these "persistent states"! – Amro Sep 23 '14 at 18:25
  • btw the R2014b version (prerelease so far) has made some improvements in this regard (changing a class file while having live objects) – Amro Sep 23 '14 at 18:31