Here is one opportunity. Let's say that I want to find all the doubles in the workspace. I could do something like this
>> x = 12.3; >> y = 45.6; >> z = '789';
Get a list of all variables in the workspace
>> vars = whos();
Find out which ones double
>> location = strcmp('double',{vars.class});
Get names
>> names = {vars(location).name}; >> names names = 'x' 'y'
If you want to get an array of some property x (let's say I want to get the cosine of each double), you can do something like this
>> N = length(names); >> arr = NaN(1,N); >> for n = 1:N obj = eval(names{n});
Now you have
>> arr arr = 0.9647 -0.0469
Here is an example of using a custom object. Firstly, I put this code in a DProtein.m file
classdef DProtein properties x; y; end methods function self = DProtein(x, y) self.x = x; self.y = y; end end end
Now I am creating a couple of objects
>> a = DProtein(1, 'foo');
I find all objects of the correct class in the workspace as before
>> vars = whos(); >> location = strcmp('DProtein', {vars.class}); >> names = {vars(location).name};
Now the loop collects an array of each object
>> for n = 1:length(names) objects(n) = eval(names{n});
And you can collect all properties like this
>> [objects.x] ans = 1 2