Question

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');
Was it helpful?

Solution 2

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.

OTHER TIPS

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top