×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to create array of user objects?

How to create array of user objects?

How to create array of user objects?

(OP)
I've created a fairly simple class and can instantiate and work with objects of the class. Now someone has asked for the ability to create arrays of objects, which I thought would be straight-forward, but I'm getting errors from the subsref.m function.
 
I don't even understand why subsref is being called. I'm attempting to create an array as follows:
 
obj1 = myClass(args);
obj2 = myClass(args);
objArray = [obj1,obj2];
 
If I then ask for objArray(1), I get subsref errors, apparently because the objArray is of type myClass.
 
I thought subsref was for handling index references INTO an object, e.g. obj1(1), which might well return a value of a different type, whereas this is a case where I want to index into an array and get a myClass object as a return value.
 
What am I misunderstanding? Is there a way to assign objArray = [obj1,obj2];?
 
thanks,
Bruce

RE: How to create array of user objects?

(OP)
I found that my problem came from my having created stub member functions overriding the built-in subsref.m and subsasgn.m functions.  I did this just to print meaningful warning messages to the screen, since I had thought that Matlab would fail with no helpful error messages otherwise.

It turns out that if I just leave these functions out all together -- which I could easily do since they were doing nothing useful -- then Matlab calls the built-in versions, and everything works fine.

I hope this will be helpful to someone.

- Bruce

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources