How to assign to a collection of object properties stored in an object array, using a numerical array of compatible type and size (one statement, without for-loop!)?
Show older comments
I am building a minimal discrete-event simulation support, where sequences of events will be initialized from external weather records (of precipitation, for example). Class EventList_MWE (subscript '_MWE' denoting 'minimum working example') implements the DEVS list using a circular array (buffer) of events of base class MySimEvent, from which I derive user-specific event classes and methods, such as DerivedSimEvent. To handle polymorphic events, MySimEvent inherits from the class matlab.mixin.Heterogeneous.
The event list class is an aggregation of an object array of MySimEvent objects (objArray) and has further properties, but is not inherited from an array class. The constructor EventListExp(N) initializes the buffer with N events with undefined (NaN) properties:
objArray(1:N) = MySimEvent().
Events are currently Value, not Handle, class objects; and event properties are kept public to be accessible from EventListExp.
The error occurs in my method EventListExp.ArrayInit(obj, T, ID), which initializes a subset of events using as input the arrays T and ID:
[obj.objArray(IndexSet).tstmp] = T(1:nI);
Error: Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
IndexSet is an array of valid indices of the same size as T(1:nI) (in my example: 10); both arrays are double-typed.
I also tried subsasgn function and defined a “substruct” structure argument, but I do not yet overload / override the function:
subs = substruct('.','objArray', '()',{IndexSet}, '.','tstmp');
obj = subsasgn(obj, subs, T(1:nI));
Error: Expected one output from a curly brace or dot indexing expression, but there were 10 results.
Questions: Isn't the property collection (array) enclosed in square brackets '[]' a valid target for an assignment (i.e. a vallid left-hand-side)? Do I wrongly use subsasgn for indexing, or must I overwrite subsasgn (in which class?)? Or is my approach pointless, since no performance will ever be gained compared to iterating event constructor calls?
rng('shuffle');
N = 25;
DEVS_List_Test = EventList_MWE(N);
for i=1:10
DEVS_List_Test.InsertEvent(randi(10,1), i, 1, rand());
end
T = rand(1,N);
ID = randi(100,1,N);
DEVS_List_Test.ArrayInit(T, ID);
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!