241 The common application of cell arrays is the creation of text arrays

# 241 the common application of cell arrays is the

This preview shows page 55 - 58 out of 68 pages.

241 The common application of cell arrays is the creation of text arrays. Consider the following example: 52
>> M = {’January’;’February’;’March’;’April’;’May’;’June’;’July’;’August’; ’September’;’October’;’November’;’December’}; >> fprintf (’It is %s.\n’, M{9}); It is September. Exercise 81. Exercise with the concept of a cell array, first by typing the examples presented above. Next, create a cell array W with the names of week days, and using the command fprintf , display on screen the current date with the day of the week. The goal of this exercise is also to use fprintf with a format for a day name and a date, in the spirit of the above example. 9.2 Structures Structures are Matlab arrays with data objects composed of fields . Each field contains one item of informa- 245-249 tion. For example, one field might include a string representing a name, another a scalar representing age or an array of the last few salaries. Structures are especially useful for creating and handling a database. One of the possible ways to create a structure is by assigning data to individual fields. Imagine that you want to construct a database with some information on workers of a company: >> worker.name = ’John Smith’; >> worker.age = 35; >> worker.salary = [5900, 6000, 6100]; >> worker = name: ’John Smith’ age: 35 salary: [5900 6000 6100] In this way, a 1 × 1 structure array worker with three fields: name , age and salary is constructed. To expand the structure, add a subscript after the structure name: >> worker(2).name = ’Linda Murphy’; % after this, a 2nd subarray is created >> worker(2).age = 41; >> worker(2).salary = [7301, 7301]; % field sizes do not need to match! >> worker 1x2 struct array with fields: name age salary Since the structure has now size 1 × 2, Matlab does not display contents of all fields. The data is now organized as follows: worker array worker(1) .name John Smith .age 35 .salary 5900, 6000, 6100 worker(2) .name Linda Murphy .age 41 .salary 7301, 7301 Structures can also be build by using the struct function, for example: >> employee=struct( ’name’,’John Smith’,’age’,35,’salary’,[5900, 6000, 6100]); To access an entire field, include a field name after a period. To access a subarray, follow the standard way of using subscripts: 53
>> worker(1).age 35 >> worker(2).salary(2) 7301 >> worker(2) name: ’Linda Murphy’ age: 41 salary: [7301 7301] An alternative way is to use the getfield function: >> getfield(worker,{2},’salary’) 7301 7301 There exist also the function setfield , which allows assigning values . New fields can be added or deleted 248 from every structure. To delete a field, use the command rmfield . Analyze the following example: >> worker2 = worker; >> worker2 = rmfield (worker, ’age’); >> worker2(2).street = ’Bakerstreet 5’; >> worker2(2) name: ’Linda Murphy’ salary: [7301 7301] street: ’Bakerstreet 5’ >> worker2(1) name: ’John Smith’ salary: [5900 6000 6100] % in all other substructures address field is empty street: [] % you should assign new values for each substructure Operating on fields and field elements is done in the same way as on any other array. Consider the following example, where the average salary for each worker is computed.

#### You've reached the end of your free preview.

Want to read all 68 pages?

• Fall '13
• Ivan
• matlab, .........