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 7278461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:52:40+00:00 2026-05-28T22:52:40+00:00

I am hoping to have a multidimensional array of structures, but I can’t seem

  • 0

I am hoping to have a multidimensional array of structures, but I can’t seem to get at the field of the contained elements. or in code:

mySample = struct('a', zeros(numA),'b', zeros(numB));
Data = cells(height,width);
disp(Data(1,1).a);

The bottom line fails with an error such as

“Improper index matrix reference.”

How is a 2D array of structures done in Matlab?

  • 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-05-28T22:52:41+00:00Added an answer on May 28, 2026 at 10:52 pm

    There are a couple of ways to create an array of structures (“structure array” or “struct array”). Note that in a struct array, each element must have the same fields. For example, if s(1) has fields “a” and “b”, then s(2)..s(n) must have fields “a” and “b”.

    % num rows
    n = 10;
    % num cols
    m = 50;
    
    % method 1, which will repeat a structure
    s = struct('field1', 10, 'field2', 20);
    sArray = repmat(s, n, m);
    
    % method 2, which initializes each field to empty []
    sArray(n,m) = struct('field1', [], 'field2', []);
    

    You can expand on that to go beyond the second dimension eaisly:

    sArray(n,m,p) = struct('field1', [], 'field2', []);
    

    You could also preallocate the array and use a for-loop to set the value of each field. Additionally:

    help deal
    help structfun
    

    You could also create a cell array of structures, which provides more flexibility: each structure in the cell array may have different fields.

    c = cell(1,2);
    c{1} = struct('a', 1, 'b', 2);
    c{2} = struct('z', 0, 'q', 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I'm hoping to get some direction in solving -
I have this piece of code which I'm hoping will be able to tell
i have a few question on php db connection and hoping someone can answer
I have a question regarding the OnItemDataBound event and I'm hoping someone can help
I have a small problem that I am hoping to get some help with.
I am wondering how to use apply on a multidimensional array. I have something
I have some multidimensional data structures that I need to do some interpolation on.
I'm trying to simulate some code that I have working with SQL but using
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
I am aware that javascript does not allow true multidimensional arrays but you can

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.