2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2011 - DIGITEO - Antoine ELIAS
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
16 // <-- CLI SHELL MODE -->
20 firstname = "firstname";
21 firstname_new = "firstname_new";
22 lastname = "lastname";
23 lastname_new = "lastname_new";
25 email_new = "email_new";
26 phone_new = "phone_new";
28 // create a one dimensional struct
29 st = struct("firstname", firstname, "lastname", lastname, "email", email);
31 assert_checkequal(st.firstname, firstname);
32 assert_checkequal(st.lastname, lastname);
33 assert_checkequal(st.email, email);
36 st.firstname = firstname_new;
37 st.lastname = lastname_new;
43 assert_checkequal(st.firstname, firstname_new);
44 assert_checkequal(st.lastname, lastname_new);
45 assert_checkequal(st.email, email_new);
46 assert_checkequal(st.phone, phone_new);
49 //create multi-dimentional struct (4x3x2)
54 firstnames{n1,n2,n3} = [];
55 lastnames{n1,n2,n3} = [];
56 emails{n1,n2,n3} = [];
61 firstnames{i} = "firstname_" + sz;
62 lastnames{i} = "lastname_" + sz;
63 emails{i} = "email_" + sz;
66 st = struct("firstname", firstnames, "lastname", lastnames, "email", emails);
69 if st(i).firstname <> firstnames{i} then pause end
70 if st(i).lastname <> lastnames{i} then pause end
71 if st(i).email <> emails{i} then pause end
78 //creating one dimensional structure by insertion
79 firstname = "firstname";
80 lastname = "lastname";
83 st.firstname = firstname;
84 st.lastname = lastname;
87 assert_checkequal(st.firstname, firstname);
88 assert_checkequal(st.lastname, lastname);
89 assert_checkequal(st.email, email);
92 //create multi dimentional struct by insertion
97 firstnames(n1,n2,n3) = "";
98 lastnames(n1,n2,n3) = "";
99 emails(n1,n2,n3) = "";
104 firstnames(i) = "firstname_" + sz;
105 lastnames(i) = "lastname_" + sz;
106 emails(i) = "email_" + sz;
109 //set dimension to 4x3x2
110 st(4,3,2).firstname = firstnames($);
111 st(4,3,2).lastname = lastnames($);
113 //fill struct as vector
115 st(i).firstname = firstnames(i);
116 st(i).lastname = lastnames(i);
117 st(i).email = emails(i);
120 //check data on each dimension
124 assert_checkequal(st(i,j,k).firstname, firstnames(i,j,k));
125 assert_checkequal(st(i,j,k).lastname, lastnames(i,j,k));
126 assert_checkequal(st(i,j,k).email, emails(i,j,k));
135 //fields of an empty struct
136 fields = getfield(1,struct());
137 assert_checkequal(fields(1), "st");
138 assert_checkequal(fields(2), "dims");