Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 6

Task 6.5 Detail: Manipulating and using character variables.

Summary of new tools and commands.

Task: Read a set of data files with different names and call the data by a different variable name. The files are FILE=['Data1';'Data2';Data3']; and the variable names are VAR=['d1';'d2';'d3'];

Because there is a pattern in the data file names and the variable names, this task would seem to be easy. We can certainly read the different data files using the concatenation operator to construct the file names. However, we don't know how to have variable names determined by character strings.

There is a special MATLAB command, eval(), which takes as its argument a character string and considers it to be a command. For example, we could do any of the following:

COMMAND='A=1;';eval(COMMAND);
eval('A=1;');
eval(['A' '=' '1' ';']);
The first statement creates a variable A with the value 1. The second line does the same task, as does the third line.

Character variables in MATLAB are simply arrays of single characters. These variables follow the same indexing schemes as numerical variables. We can create a character array by surrounding characters with a single quote,

  C='ABCD E';
  D='12345';
The C has 6 characters in it (blank is a character!) and D has 5 characters in it. disp(C(3)) will print the single character "C", while disp(D(2:5)) will print "2345".

We can create 2D array of characters with

P=['VR1';'VR2';'VR3';'VR4'];
Recall that ; signals the end of a row in a concatenation operation. Printing the variable P yields
 P =
   VR1
   VR2
   VR3
   VR4
Using what we know about array indexing, disp(P(2,3)) returns the character "2", which is in the second row and third column. The command disp(P(2,:)) returns the string "VR2", all characters in the second row.

We can use this capability to work on a list of files. First, create a list of file names,

FILES=['CTD1';'ctd3';'CTD8';'ctd9'];
all of which end in '.dat'.

Notice that there is almost a pattern to these names, but it would be painful to program a name creator. In this case, just put the names in a variable (and send a nasty-gram to whoever created these data files with these names).

You can now work with these files in sequence,

DIR='DATA/Cruise3/';
for i=1:4
  FILENAME=[DIR FILES(i,:) '.dat'];
  data=load(FILENAME);
%    commands for whatever you need to do with the data
end

One serious problem with this idea, is that all of the file names must have the same number of characters. This programming idea would not work if there were files with names like "ctd1", "ctd10" and "ctd100", because they have different numbers of characters. We will see an alternative technique later for this situation using cell arrays.

There is a solution to this problem using num2str by including a format specfication. The file names can be created with FILE=['CTD' num2str(N,'%1d') '.dat'];. The format as the second argument in num2str, controls how the number is converted to a character string. The format is adjusted if the number is too big for the number of spaces indicated.

N=1;FILE=['CTD' num2str(N,'%1d') '.dat']; produces the string "CTD1.dat".

N=10;FILE=['CTD' num2str(N,'%1d') '.dat']; produces the string "CTD10.dat".

N=100;FILE=['CTD' num2str(N,'%1d') '.dat']; produces the string "CTD100.dat".

There are many other ways to work with characters and character variables. The two ideas presented here involve 1) construction of commands which are invoked with eval() and 2) defining lists of file names to do repetative commands on data files with different names.

Flow chart to complete the task:

%%%  Set up file and variable names
%%%  set the number of files to work with
%%%  loop over file names
%%%     read file and save contents to a variable

Script to complete the task:

%%%  Set up file and variable names
FILENAME=['Data1'; 'Data2'; 'Data3'];
VAR=['d1';'d2';'d3'];
%%%  set the number of files to work with
nF=3;
%%%  loop over file names
for file=1:nF
%%%     read file and save contents to a variable
  COMMAND=[VAR(file,:) '=load(' FILENAME(file) ');'];
  eval(COMMAND);
end
%   variables are available to work with

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 6


email: J. Klinck