0

A basic question for which I need some clarifications:

I have a problem statement - "Create a variable s is a 3-by-4 structure with fields a, b, and c. Each of these fields is a 2-by-5 array of class double"

So first I create a 2-by-5 array of class double :

>> d = [1.0 2.0 3.0 4.0 5.0; 1.0 2.0 3.0 4.0 5.0]

The 4 structures :

s1 = struct('a', d,'b',d,'c', d)
s2 = struct('a', d,'b',d,'c', d)
s3 = struct('a', d,'b',d,'c', d)
s4 = struct('a', d,'b',d,'c', d)

Then the final structure:

>> S = [s1 s2 s3 s4;s1 s2 s3 s4;s1 s2 s3 s4]

S = 

3x4 struct array with fields:
    a
    b
    c

Does the above example meets the problem statement - is there any difference in-between "3-by-4 structure" and "3x4 struct array" as per example I mentioned - if yes then how do I create a 3-by-4 structure ?

Also if the above example is correct then I am confused of output of below commands:

S, S(:), S(2:3,1:3), S(2,3).a and S(2:3,3:4).a

>> S

S = 

3x4 struct array with fields:
    a
    b
    c

>> S(:)

ans = 

12x1 struct array with fields:
    a
    b
    c

What are the values returned and how can I see them? Note

>> S(:)(1)
??? Error: () Indexing must appear last in an index expression.

If S(:) returns a 12x1 struct array with fields

>> S(2:3,1:3)

ans = 

2x3 struct array with fields:
    a
    b
    c

Why I am not able to get to see all elements of structure 'S'

>> S(2,3).a

ans =

     1     2     3     4     5
     1     2     3     4     5

>> S(2:3,3:4).a

ans =

     1     2     3     4     5
     1     2     3     4     5


ans =

     1     2     3     4     5
     1     2     3     4     5


ans =

     1     2     3     4     5
     1     2     3     4     5


ans =

     1     2     3     4     5
     1     2     3     4     5

>> 
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • 1
    Looks right to me. BTW you shouldn't put "MATLAB : " in your question titles, the tag is enough – Dan Jan 24 '14 at 09:17
  • Thanks I would take care of same, I have updated the question with more details – Programmer Jan 24 '14 at 09:19
  • 1
    Either use the graphical variable explorer in Matlab or else try something like this: http://www.mathworks.com/matlabcentral/fileexchange/13831-structure-display – Dan Jan 24 '14 at 09:24
  • 1
    You can build your 3x4 struct this way, as well: `S = repmat(struct('a', d, 'b', d, 'c', d), [3 4])` – Peter Jan 24 '14 at 14:23

1 Answers1

0

I think your statements are correct regarding naming, so I'll answer the output part:

First off: You won't get to see the values of each field, unless you specifically ask to see it, i.e. you specify the field such as: S(1).a, or S(1,3).b. This goes for all the below answers. If you don't specify it, you will get just the size of the array, and the names of the fields.

So:

  1. I assume this one is OK:

    S
    S = 3x4 struct array with fields: a b c

  2. This one is the same as with any array. The (:) turns the array into a vector of dimension (numel(S),1). Therefore 12x1 in this case. You will still have the same struct fields.

    S(:)
    ans =
    12x1 struct array with fields: a b c

  3. This one returns an error, because direct indexing is not allowed in MATLAB. I think this might work in Octave, but don't take my word for it, as I can't test it right now.

    S(:)(1) ??? Error: () Indexing must appear last in an index expression.

  4. Again, same as with any array. You take the 2nd and 3rd row, and 1st-3rd column of your struct array. You will still have the same fields a, b and c.

    S(2:3,1:3)
    ans =
    2x3 struct array with fields: a b c

  5. Now you ask to see the elements in field a of the (2,3) element of you struct array. Again, I guess this one should be quite clear.

    S(2,3).a

    ans =

     1     2     3     4     5
     1     2     3     4     5
    
  6. Similarly, now you take all elements in fields a for the 2nd and 3rd row, and 1st-3rd column.

    S(2:3,3:4).a

    ans =

     1     2     3     4     5
     1     2     3     4     5
    

    ans =

     1     2     3     4     5
     1     2     3     4     5
    

    ans =

     1     2     3     4     5
     1     2     3     4     5
    

    ans =

     1     2     3     4     5
     1     2     3     4     5
    

I hope this answers your question =)

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70