Home > OS >  Nested cell arrays in Scilab
Nested cell arrays in Scilab

Time:05-12

I need a one-dimensional array of structures; each structure consists of a 3*n array (the n can differ between them) and three separate real numbers.

When I try to access the elements of that structure, I get their descriptions as 3n or 11 arrays, respectively, but cannot seem to get their actual values.

Here is an example:

A = {[1 2 3; 4 5 6], 100, 200, 300}

B = {[7 8 9; 10 11 12], 101, 201, 301}

C = {[13 14 15; 16 17 18], 102, 202, 302}

ABC={A B C}

--> ABC{2}(2) ans = [1x1 constant]

--> ABC{2}{2} ABC{2}{2}

  ^^ Error: syntax error, unexpected {, expecting end of file

I'm getting the impression that I lack some fundamental understanding of what cell arrays are.

Any hints?

CodePudding user response:

Recursive extraction of cells is currently broken (see http://bugzilla.scilab.org/show_bug.cgi?id=15756). You have to make the extraction in two steps:

--> c=ABC{2}; c{2}
 ans  =

   101.

CodePudding user response:

The title speaks about cells, while the description speaks about structures. Cells and structures are 2 distinct types of data container. Here is an example with an actual 3x1 array of structures with shared fields "mainarray" "real1" "real2" "real3":

--> ABC = struct("mainarray",zeros(3,3),"real1",1,"real2",2,"real3",3)
 ABC  =
  mainarray: [3x3 constant]
  real1 = 1
  real2 = 2
  real3 = 3

--> ABC(2).mainarray = grand(3,4,"uin",0,9); ABC(2).real1=%pi; ABC(2).real2 = %e; ABC(2).real3 = %i
 ABC  =
  2x1 struct with fields:
  ["mainarray", "real1", "real2", "real3"]


--> ABC(3) = struct("mainarray", eye(3,5), "real1",8,  "real2", 10, "real3", 13)
 ABC  = 
  3x1 struct with fields:
  ["mainarray", "real1", "real2", "real3"]


--> ABC(3)
 ans  =
  mainarray: [3x5 constant]
  real1 = 8
  real2 = 10
  real3 = 13


--> ABC.real1
 ans  =
  (1) = 1
  (2) = 3.1415927
  (3) = 8


--> ABC.mainarray
 ans  =
  (1) : [3x3 constant]
  (2) : [3x4 constant]
  (3) : [3x5 constant]


--> ABC(2).mainarray
 ans  =
   2.   5.   9.   3.
   2.   4.   5.   5.
   4.   1.   8.   5.

--> ABC(2).mainarray(:,2)
 ans  =
   5.
   4.
   1.

A similar 1x3 array can be initialized with a single line of code:

--> ABC = struct("mainarray",{zeros(3,3),grand(3,4,"uin",0,9), eye(3,5)}, ..
                 "real1",{1,%pi,8}, "real2",{2,%e,10}, "real3",{3,%i,13})
 ABC  = 
  1x3 struct with fields:
  ["mainarray", "real1", "real2", "real3"]

--> ABC(2)
 ans  =
  mainarray: [3x4 constant]
  real1 = 3.1415927
  real2 = 2.7182818
  real3 = %i
  • Related