Matlab's arrayfun for uniform output of class objects


I need to build an array of objects of class ID using arrayfun:

% ID.m classdef ID < handle properties id end methods function obj = ID(id) obj.id = id; end end end

But get an error:

>> ids = 1:5; >> s = arrayfun(@(id) ID(id), ids) ??? Error using ==> arrayfun ID output type is not currently implemented.

I can build it alternatively in a loop:

s = []; for k = 1 : length(ids) s = cat(1, s, ID(ids(k))); end

but what is wrong with this usage of arrayfun?

Edit (clarification of the question): The question is not how to workaround the problem (there are several solutions), but why the simple syntax s = arrayfun(@(id) ID(id), ids); doesn't work. Thanks.


You are asking arrayfun to do something it isn't built to do.

<a href="http://www.mathworks.com/help/techdoc/ref/arrayfun.html" rel="nofollow">The output from arrayfun must be</a>:


scalar values (numeric, logical, character, or structure) or cell arrays.


Objects don't count as any of the scalar types, which is why the "workarounds" all involve using a cell array as the output. One thing to try is using cell2mat to convert the output to your desired form; it can be done in one line. (I haven't tested it though.)

s = cell2mat(arrayfun(@(id) ID(id), ids,'UniformOutput',false));


Perhaps the easiest is to use cellfun, or force arrayfun to return a cell array by setting the 'UniformOutput' option. Then you can convert this cell array to an array of obects (same as using cat above).

s = arrayfun(@(x) ID(x), ids, 'UniformOutput', false); s = [s{:}];


This is how I would create an <a href="http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html" rel="nofollow">array of objects</a>:

s = ID.empty(0,5); for i=5:-1:1 s(i) = ID(i); end

It is always a good idea to provide a "default constructor" with no arguments, or at least use default values:

classdef ID < handle properties id end methods function obj = ID(id) if nargin<1, id = 0; end obj.id = id; end end end


  • iOS: define and use C++ in Objective-C++
  • How to make this action throw a 404?
  • afterSetExtremes callback for highcharts
  • Can I run Robotium tests automatically in parallel on multiple emulators?
  • Restructure php contact form
  • Do custom action after devise model confirmation
  • Differences between drawing an Ellipse in Android and Java
  • Yii2: Using Kartik Depdrop Widget?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • Pythons argparse default value doesn't work
  • Detection of framework usage on Mac system?
  • php is_file always return false
  • Memory error in python- how to use more memory
  • C++ pointer value changes with static_cast
  • Marklogic : Query response time is very high
  • Sort List of Strings By Version
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Meteor: Do Something On Email Verification Confirmation
  • Get data from AJAX - How to
  • Knitr HTML Loop - Some HTML output, some R output
  • Deserializing XML into class C#
  • Where to put my custom functions in Wordpress?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • RestKit - RKRequestDelegate does not exist
  • retrieve vertices with no linked edge in arangodb
  • WPF Applying a trigger on binding failure
  • python regex in pyparsing
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Suggestions to manage Login/Logout transitions
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • Change div Background jquery
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • python draw pie shapes with colour filled
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML