16.5: cell array
- Page ID
- 85201
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Highest rated
(rating)Recently updated
(date updated)Recently added
(date created)
By Carey A. Smith
An ordinary array of strings can be created when all of the strings are the same length.
Cell arrays are useful for containing multiple character strings of different lengths.
%% String_array_comparison_demo
clear all, format compact, format shortg; close all; fclose all; clc;
% A string array can only be created in MATLAB if every string is the same length.
% If you try to create an array with strings of different lengths, MATLAB generates an error.
% Octave, however, will automatically pad the short strings with blanks, in order to avoid an error.
% The shapes in this example are listed in order of the number of sides.
% All the strings have been abbreviated to 3 characters,
% so that an array can be created.
shapes = ['cir' % circle (1 side)
'ban' % banana (2 sides)
'tri' % triangle (3 sides)
'squ' % square (4 sides)
'pen' % pentagon (5 sides)
'hex']; % hexagon (6 sides)
[n, m] = size(shapes)
%% Input shape name.
disp('Shapes to choose from:')
disp('triangle')
disp('square')
disp('hexagon')
disp('circle')
disp('banana')
in_shape = input('Enter the name of a shape (without quotes): ','s')
%% Match the input to the shapes array
tf = 0; % True/False
for k = 1:n
% Compare the first 3 letters of the input shape name
% to each of the names in the shapes array.
tf = strcmpi(in_shape(1:3), shapes(k,:)); % case insensitive
if(tf == 1) % If True
disp(['A ',in_shape,' has ',num2str(k),' sides.'])
break;
end
end
if(tf == 0)
disp(['No match was found for ',in_shape])
end
Solution
Add example text here.
.
Cell arrays can contain character strings of different lengths.
Cell arrays are defined by using { } ("squiggly brackets", a.k.a. "curly brackets". a.k.a. "curly braces")
The following example demonstrates how to create and use a cell array.
%% Cell_array_demonstration.m
clear all, format compact, format shortg; close all; fclose all; clc;
% MATLAB "cell arrays" allow character strings of various lengths
% to be stored in the same array.
%% This is an example of how to create a cell array.
% Note that cell arrays use squiggly brackets { }, instead of
% the square brackets [ ], used by numerical arrays.
names = {'Steven'
'Matt'
'Giovanni'
'Ashanti'
'Meagan'
'Gabriela'
'Peggy'}
names_len = length(names);
%% This shows how a cell array can be used
% [1 pt] Ask the use to enter a name
name_in = input('Enter a name from the list above (without quotes): ','s');
%% Search for match of name_in to one of those in the names list.
tf = 0; % True/False
for k = 1:names_len
name_k = names{k};
tf = strcmpi(name_in, name_k); % case insensitive comparison
if(tf == 1) % If True
disp([name_in,' is number ',num2str(k),' in the list.'])
break;
end
end
if(tf == 0)
disp(['No match was found for ',name_in])
end
Solution
Add example text here.
Cell arrays can also be used as parts of structures.
Make a cell array called South_American_Countries.
You can find a link to these countries at List of South American Countries (Links to an external site.)
In this list, put the countries called "Sovereign states" in your cell array. Do not include the territories, which are listed below the Sovereign states.
Then add this code to display the 3rd and 7th countries in your cell array:
South_American_Countries{3} % Note that you need squiggly brackets to access members of a cell array.
South_American_Countries{7}
(If your cell array has a different name, use that name.)
- Answer
-
The answer is not given here.
.
Cell arrays can be used to create a two-lin title, as shown in this example:
%% Two-ltne title with a cell array in MATLAB. (Doesn't work in Octave.)
% Mimic 70 dice rolls.
% Create a vector with uniform random numbers between 1 and 6.
nn = 70
dd = randi(6,1,70); % dice roll values
% Plot a histogram
figure;
bin_edges= (1:7)-0.5;
histogram(dd,bin_edges);
%Compute the mean of the vector.
mdice = mean(dd)
%Compute the standard deviation of the vector.
sdice = std(dd)
% Create a two line title using 2 cell arrays
title({[num2str(nn),' Dice Rolls']}, ...
{['Mean= ',num2str(mdice),', Std. Dev.= ',num2str(sdice)]});
Solution
Add example text here.
.