[MATLAB logo] Exploiting MATLAB's Data Structures

Solution #2 - Cell Array of Strings

One of the most important uses of the cell array is to hold a bunch of strings of different lengths. In this problem you will learn how to work with cell arrays of strings.

Goal: Determine the list of variables that will conflict when you load in a MAT-file. Use who and who -file to create a list of the variables in each workspace. Use setdiff or intersect to determine if there will be any name clashes.

Key concepts: working with cell arrays of strings, creating cell arrays of strings, operating on cell arrays of strings.

Key functions: who, whos, load, intersect, strmatch

Solution

  1. The file topo.mat contains matrices that can be used to display a topographic map. Use who -file (or whos -file) to determine the variables in the file before loading them.
    who -file topo
  2. Clear the workspace (with clear) and load the mat file.
    clear
    load topo
  3. The file world.mat also contains matrices that can be used to display a map. We'd like to load this file as well but we need to make sure that the variables in the file don't conflict with what we have in the workspace already. Use who and who -file with an output argument to get the list of variables in the workspace and file into cell arrays of strings (remember to use the functional form: s = who('-file',filename)).
    work = who
    file = who('-file','world')
  4. Use the function intersect or the function strmatch to determine if any of the variables in world.mat will conflict with the variables in the workspace. If so, what are the names of the clashing variables?
    conflicts = intersect(work,file)
    or
    conflicts = {};
    for i=1:length(work)
    k = strmatch(work{i},file,'exact')
    if ~isempty(k)
    conflicts = [conflicts;file(k)];
    end
    end
    The solution that uses strmatch would be preferred if partial matches are allowed. Just get rid of the 'exact' flag.
    The conflicting variable is 'topo'.

Copyright © 1997 The MathWorks, Inc.
clay@mathworks.com
$Revision$ $Date$