2

I want an OOP like interface with Matlab, in a way that I have something like

classdef Foo
 properties (Constant)
  dict = NaN;
 end
 methods (Static)
  function list = search(varargin)
   %...
   Foo.dict = cell(10,5); % etc..
  end
 end
end

So I can access and modify the (static) property. How should I set it? Not Constant I guess..

Update: My problem is that t9.load(); does not update t9.dict

classdef t9
    %T9 Summary of this class goes here
    %   Detailed explanation goes here

    properties% (Static)
        dict = {};
    end

    methods (Static)
        function load()
            load('usdict.mat');
            t9.dict = usdict;
        end
        function matches = search(varargin)
            matches = {};
            num_words = nargin-1;
            for word=t9.dict(num_words, :)
                if strcmp(word,'')
                    continue;
                end
                word_cell = word{1};
                success = true;
                for i=1:num_words
                    char = word_cell(i);
                    if ~ismember(char, varargin{i})
                        success = false;
                    end
                end
                if success, matches{end+1} = word_cell; end
            end
        end
    end

end

Wishes:

t9.load();
t9.search('ABC', 'DEF');
t9.search('DEF', 'GHI', 'MNO');
chappjc
  • 30,359
  • 6
  • 75
  • 132
casparjespersen
  • 3,460
  • 5
  • 38
  • 63
  • `Static` `methods` are methods which can be run without an instance of the class; they can only refer to `constant` `properties`. Setting a `property` of a class requires an instance of that class. So, it would seem your `method` should not be `static`. – Rody Oldenhuis Feb 06 '14 at 14:34
  • @RodyOldenhuis Look at my updated code. – casparjespersen Feb 06 '14 at 14:37
  • I'm sorry to say, but it sounds to me like you're creating [spaghetti with meatballs](http://en.wikipedia.org/wiki/Spaghetti_code#Spaghetti_with_meatballs) here... – Rody Oldenhuis Feb 06 '14 at 16:16
  • You can do it with the `Constant` attribute, if that points to an instance of a class, which is initialised in the properties section and thus shared among your t9 instances. See my answer to this question here: http://stackoverflow.com/a/33922082/942179. – Elmar Zander Nov 25 '15 at 17:08

3 Answers3

2

I would maintain dict as a Dependent property and have it reference a persistent variable containing the static data, as below. With this, you can update dict, using either the constructor or by setting it directly using an object instance. All instances will share whatever value you give it, even when loaded from a .mat file.

 classdef myclass

 properties (Dependent) 
     dict   
 end

 methods

     function obj=set.dict(obj,val)

          manageDict('set',val);

     end

     function val=get.dict(obj)

         val=manageDict('get');

     end

     function obj=myclass(dict)

         obj.dict=dict;


     end

 end

end


function varargout=manageDict(op,newdict)

   persistent dictStatic;

   switch op

       case 'get'

           varargout{1}=dictStatic;

       case 'set'    

           dictStatic=newdict;

   end


end
Matt J
  • 1,127
  • 7
  • 15
1

If you're never going to add to the dictionary, then the simplest thing to do is to load the dictionary as a constant property.

classdef t9
    properties (Constant)
        dict = i_loadDict()
    end
    ...
end

function out = i_loadDict()
    load('usdict.mat');
    out = usdict;
end

There are ways to have something updateable, but this is the simplest solution to your problem.

Nzbuu
  • 5,241
  • 1
  • 29
  • 51
1

I really don't understand why you want to avoid having an instance...why not use this:

T = t9;
T.load(); %// taken care of by constructor?

T.search('ABC', 'DEF');
T.search('DEF', 'GHI', 'MNO');

clear T

instead of your wishlist? The classdef:

classdef t9
    %T9 Summary of this class goes here
    %   Detailed explanation goes here

    properties
        dict = {};
    end

    methods %// note: NOT static

        function load(obj)
            load('usdict.mat');
            obj.dict = usdict;
        end

        function matches = search(obj, varargin)
            matches = {};
            num_words = nargin-1;
            for word=obj.dict(num_words, :)
                if strcmp(word,'')
                    continue;
                end
                word_cell = word{1};
                success = true;
                for i=1:num_words
                    char = word_cell(i);
                    if ~ismember(char, varargin{i})
                        success = false;
                    end
                end
                if success, matches{end+1} = word_cell; end
            end
        end
    end

end
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Because I will use it from several n different scripts and I don't want to instantiate it several n times. – casparjespersen Feb 06 '14 at 15:32
  • @c.jespersen: Why not create one instance and pass that around to all scripts? You can adjust the dictionary to suit your needs, as shown by Matt. If you're going to use object orientation, use *objects*. Otherwise, just use a function. – Rody Oldenhuis Feb 06 '14 at 16:10
  • Because the solutions that NZbuu and Matt J has provided are smarter and easier than passing the instance along everywhere I need to use it :-) – casparjespersen Feb 06 '14 at 16:11
  • 1
    "easier": yes. "smarter": ...I'll ask you again in a couple of months :) – Rody Oldenhuis Feb 06 '14 at 16:17
  • Why should it not be smarter? Now I know that I can reach it as t9.search() at any time. – casparjespersen Feb 06 '14 at 16:27