Skip to main content
Library homepage
 
Engineering LibreTexts

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}}\)

    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.

    Example \(\PageIndex{1}\) String Array Shape Comparison

    %% String_array_comparison_demo
    clear all, format compact, format shortg; close all; fclose all; clc;
    % A string array can only be created if every string is the same length
    % These shapes are listed in order of the number of sides.
    % All the string have been abbreviated to 3 characters,
    % so that and arrya 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.

    Example \(\PageIndex{2}\) Cell Array Name Comparison

    %% 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.

    Exercise \(\PageIndex{1}\) Cell Array of South American Countries

    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:

    Example \(\PageIndex{1}\) Cell Arrays in Figure Titles

    %% Two-litne title with a cell array
    % 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.

    .


    This page titled 5: cell array is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.