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.
15 // <-- CLI SHELL MODE -->
17 firstname = "firstname";
18 firstname_new = "firstname_new";
19 lastname = "lastname";
20 lastname_new = "lastname_new";
22 email_new = "email_new";
23 phone_new = "phone_new";
24 // create a one dimensional struct
25 st = struct("firstname", firstname, "lastname", lastname, "email", email);
26 assert_checkequal(st.firstname, firstname);
27 assert_checkequal(st.lastname, lastname);
28 assert_checkequal(st.email, email);
30 st.firstname = firstname_new;
31 st.lastname = lastname_new;
35 assert_checkequal(st.firstname, firstname_new);
36 assert_checkequal(st.lastname, lastname_new);
37 assert_checkequal(st.email, email_new);
38 assert_checkequal(st.phone, phone_new);
40 //create multi-dimentional struct (4x3x2)
44 firstnames{n1,n2,n3} = [];
45 lastnames{n1,n2,n3} = [];
46 emails{n1,n2,n3} = [];
49 firstnames{i} = "firstname_" + sz;
50 lastnames{i} = "lastname_" + sz;
51 emails{i} = "email_" + sz;
53 st = struct("firstname", firstnames, "lastname", lastnames, "email", emails);
55 if st(i).firstname <> firstnames{i} then pause end
56 if st(i).lastname <> lastnames{i} then pause end
57 if st(i).email <> emails{i} then pause end
63 //creating one dimensional structure by insertion
64 firstname = "firstname";
65 lastname = "lastname";
67 st.firstname = firstname;
68 st.lastname = lastname;
70 assert_checkequal(st.firstname, firstname);
71 assert_checkequal(st.lastname, lastname);
72 assert_checkequal(st.email, email);
74 //create multi dimentional struct by insertion
78 firstnames(n1,n2,n3) = "";
79 lastnames(n1,n2,n3) = "";
80 emails(n1,n2,n3) = "";
83 firstnames(i) = "firstname_" + sz;
84 lastnames(i) = "lastname_" + sz;
85 emails(i) = "email_" + sz;
87 //set dimension to 4x3x2
88 st(4,3,2).firstname = firstnames($);
89 st(4,3,2).lastname = lastnames($);
90 //fill struct as vector
92 st(i).firstname = firstnames(i);
93 st(i).lastname = lastnames(i);
94 st(i).email = emails(i);
96 //check data on each dimension
100 assert_checkequal(st(i,j,k).firstname, firstnames(i,j,k));
101 assert_checkequal(st(i,j,k).lastname, lastnames(i,j,k));
102 assert_checkequal(st(i,j,k).email, emails(i,j,k));
110 //fields of an empty struct
111 fields = getfield(1,struct());
112 assert_checkequal(fields(1), "st");
113 assert_checkequal(fields(2), "dims");