Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:10:40+00:00 2026-06-18T10:10:40+00:00

I am working with arrays of structs and objects in Matlab. I want to

  • 0

I am working with arrays of structs and objects in Matlab. I want to set properties for all the members of a certain array as fast as possible.

For the problem of setting a certain struct field, I reached a solution that involves using arrayfun and setfield. The following works like a charm:

myStru.id = 0;
myStru.name = 'blah';
arrayStru = repmat(myStru,10,1);  % Array of 10 elements. All of them have id=0
arrayStru = cell2mat( arrayfun( @(x,y)setfield(x,'id',y), arrayStru, (1:10)', 'UniformOutput', false ) ); % ids ranging from 1 to 10 :D

The problem is that, for objects, this does not work. I understand that setfield is for structures, so I have tried some other alternatives. The most excruciating error pops out when I try the following:

arrayfun( @(x,y) eval(['x.id=y;']), arrayOfObjects, arrayOfValues, 'UniformOutput', false );

(The class is a very simple one, which accepts empty constructor and has a real public property called ‘id’). It results in:

Error using setFieldOfStructArray>@(x,y)eval(['x.id=y;']) (line 17)
Error: The expression to the left of the equals sign is not a valid target for an
assignment.

ALTHOUGH if I put a breakpoint in that line, it seems that the expression can be executed with the expected effects.

My two (three) questions:

  • Why does the above solution fail? How can I get that to work?
  • My final goal is to set properties fast and simple in arrays of objects. Which is the best technique for this?

(Note: I can write loops, but I always feel itchy when I have to do that :P)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:10:41+00:00Added an answer on June 18, 2026 at 10:10 am

    I think the problem may be that your propety may be readonly because setfield works also for classes.

    Anyway there is some alternative, if your class inherit from hgsetget you can use set instead of setfield.

    You can also use

    subsasgn(x,struct('type','.','subs','id'),y) 
    

    instead of

    setfield(x,'id',y)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with an array of 2D arrays in Excel VBA. I have
I am working on a program and need to make an array of objects,
I am working with multi-dimentioned arrays of bool , int , and various struct
I'm having a hard time working with arrays. Coming from AS2/AS3 and garbage collection
I really wish Processing had push and pop methods for working with Arrays, but
I am working with two-dimensional arrays of 16-bit integers defined as int16_t e[MAX_SIZE*MAX_NODE][MAX_SIZE]; int16_t
I'm working on a project where Arrays are the default data structure for everything,
Why when working with two dimensional arrays only second dimension is important for a
I have been working with listviews and arrays, what I am struggling with is
I'm getting confused working with character string arrays. I'm trying to fill 2 arrays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.