Calling properties on an array of objects

6 views (last 30 days)
greeneng
greeneng on 6 May 2016
Commented: greeneng on 6 May 2016
Hello everyone,
I have an array with objects of a class I created myself: The array is called Users and the elements in this array are from type User, a class I wrote myself. I now want to update a property for every object in the array. Currently, I am solving this issue by using a for loop. However, I've tried to replace the loop with a vector operation like this:
Users(:).propertyName = operation(....)
I received the following error message:
Expected one output from a curly brace or dot indexing
expression, but there were 100 results.
Is there a way I could still solve my issue? Or will I have to stick with the for loop, which is pretty time-consuming?
Thanks in advance

Answers (1)

Walter Roberson
Walter Roberson on 6 May 2016
[Users(:).propertyName] = operation(....)
If the operation does not have the same number of outputs as the number of entries in the array, then you might need
[Users(:).propertyName] = deal(operation(....))
  1 Comment
greeneng
greeneng on 6 May 2016
Now if the operation is a a non-static method of the object, how does it work? So:
[Users(:).propertyName] = Users(:).methodFromUserClass(...);
where the method I call is a non-static method from class User that I want to call for each object of the array Users. When I do it like this,
[Users(:).propertyName] = [Users(:)].methodFromUserClass(...);
I can't apply it because it says that the dot behind ] is invalid Matlab syntax.
Thanks in advance!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!