Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <a href="http://www.mathworks.com/help/techdoc/ref/what.html" rel="nofollow">WHAT</a> function to discover classes (as well as functions,packages,etc...) in a certain folder, then call <a href="http://www.mathworks.com/help/techdoc/ref/methods.html" rel="nofollow">METHODS</a> to find the signature of the constructor of the class (some parsing needed here), finally using <a href="http://www.mathworks.com/help/techdoc/ref/feval.html" rel="nofollow">FEVAL</a> (passing arguments if any) to create an object from this class.</p> <p>You could also use <a href="http://www.mathworks.com/help/techdoc/ref/meta.class.html" rel="nofollow">meta.class</a> to get all sorts of meta-information about your classes.</p> <hr> <h1>EDIT</h1> <p>Here is some code to illustrate what I had in mind:</p> <pre><code>%# folder containing your classes pathName = fullfile(pwd,'folder'); %# make sure it is on the path p = textscan(path, '%s', 'Delimiter',';'); p=p{1}; if ~any(ismember(p,pathName)) addpath(pathName) end %# list MATLAB files w = what(pathName); %# get class names fNames = cellfun(@(s) s(1:end-2), w.m, 'Uni',false); %# remove .m extension fNames = [fNames ; w.classes]; %# add classes in @-folders %# get classes metadata mt = cellfun(@meta.class.fromName, fNames, 'Uni',false); %# get meta-data mt = mt( ~cellfun(@isempty,mt) ); %# get rid of plain functions %# build object from each class objects = cell(numel(mt),1); for i=1:numel(mt) %# get contructor function ctorMT = findobj(mt{i}.MethodList, 'Access','public', 'Name',mt{i}.Name); %# get number of contructor arguments numArgs = numel(ctorMT.InputNames); %# create list of arguments (using just zeros) args = repmat({0}, [numArgs 1]); %# create object try obj = feval(ctorMT.Name,args{:}); catch ME warning(ME.identifier, ME.message) obj = []; end %# store object objects{i} = obj; end </code></pre> <p>As you can see, I found it easier to simply use <code>meta.class</code> to get metadata about the classes, instead of manually parsing the output of <code>methods('fcn','-full')</code> as I originally suggested.</p> <p>However this is not perfect, as there is no way to find out what type of input each constructor expect (only how many). I opted to always pass <code>0</code> for each argument..</p> <p>To test the implementation above, I create these sample classes (one in a self-contained file, the other defined in @-folder with multiple files):</p> <h3>folder/hello.m</h3> <pre><code>classdef hello properties name = ''; end methods function this = hello() this.name = 'world'; end function val = get.name(obj) val = obj.name; end function obj = set.name(obj,val) obj.name = val; end function say(obj) fprintf('Hello %s!\n',obj.name); end end end </code></pre> <h3>folder/@hello2/hello2.m</h3> <pre><code>classdef hello2 properties name end methods function this = hello2(val) this.name = val; end function val = get.name(obj) val = obj.name; end function obj = set.name(obj,val) obj.name = val; end end methods say(obj) end end </code></pre> <h3>folder/@hello2/say.m</h3> <pre><code>function say(obj) fprintf('Hello2 %s!\n', obj.name); end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload