0% found this document useful (0 votes)
8 views

Introduction To Computing For Engineers: Cell Arrays and Examples

This document discusses cell arrays in MATLAB and provides examples of how to use them. It explains that cell arrays allow the grouping of arrays of different sizes and types into a single variable. The key points are: 1) Cell arrays use curly brackets {} instead of square brackets to concatenate arrays of different sizes into a single variable. 2) Each element of a cell array can contain a different type and size of array. 3) An example reads names from a text file into a cell array, with each name as a separate element.

Uploaded by

Châu Tú
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Introduction To Computing For Engineers: Cell Arrays and Examples

This document discusses cell arrays in MATLAB and provides examples of how to use them. It explains that cell arrays allow the grouping of arrays of different sizes and types into a single variable. The key points are: 1) Cell arrays use curly brackets {} instead of square brackets to concatenate arrays of different sizes into a single variable. 2) Each element of a cell array can contain a different type and size of array. 3) An example reads names from a text file into a cell array, with each name as a separate element.

Uploaded by

Châu Tú
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Computing

for Engineers
Cell Arrays and Examples

Why does this not work?

>>A=blue;
>>B=red;
>>C=green;
>> colors=[A; B; C];
??? Error using ==> vertcat
All rows in the bracketed expression must
have the same
number of columns.

Why does this not work?

>>A=blue;
>>B=red;
>>C=green;
>> colors=[A; B; C];
>> whos

Name
Size
A
1x4
B
1x3
C
1x5

Bytes Class
8 char array
6 char array
10 char array

How do we solve this problem?


I want a list of words and I want to save them as a
single variable (i.e. colors, names, )

>>A=blue;
>>B=red;
>>C=green;
>> colors=[A; B; C];
>> whos

Name
Size
A
1x4
B
1x3
C
1x5

Bytes Class
8 char array
6 char array
10 char array

SOLUTION: CELL ARRAYS


>>A=blue;
>>B=red;
>>C=green;
>> colors={A; B; C};
>> whos
Name
Size
A
1x4
B
1x3
C
1x5
color
3x1

Curly bracket indicates a cell array

Bytes Class
8 char array
6 char array
10 char array
204 cell array

So What is a Cell Array?


A cell is a collection of arrays of various sizes

and types.
>> A={string array, [1:.1:100],ones(100,100)}
>>A{1}=string array
>> A{2}=[1:.1:100];
>> A{3}=ones(100,100);

Example: Cell Array


Use the textread function to read a column of names in the text file names.txt
Jones
Smith
Collins
Portis
James
>> [names] = textread( names.txt', '%s' );
>> whos
Name
Size
Bytes Class
names
5x1
356 cell array
Grand total is 33 elements using 356 bytes
Variable names is a cell array
>> names{1}
ans =Jones
>> names{2}
ans=Smith

Programming Examples
1. Using a Matlab function file compute the value of pi using the following series

How many terms are needed to obtain an


accuracy of 1e-12? How accurate is the sum
of 100 terms of this series?

Programming Examples

You might also like