logo for matrixlab-examples.com
leftimage for matrixlab-examples.com


Merge Text Files – how to read txt and mat files in Matlab

We’re going to review easy ways to merge text files in Matlab. This is very useful if we want to work with available data that was generated by other software or programs. 

There are some special commands for that purpose. Let’s say that we have a plain ASCII file named results.txt. It contains the following numbers (7 rows and 4 columns):

 13.6519   58.2792   20.9069   41.5375
  1.1757   42.3496   37.9818   30.4999
 89.3898   51.5512   78.3329   87.4367
 19.9138   33.3951   68.0846    1.5009
 29.8723   43.2907   46.1095   76.7950
 66.1443   22.5950   56.7829   97.0845
 28.4409   57.9807   79.4211   99.0083




The first useful command we’re going to start with is load filename, which retrieves all variables from a file given a full pathname.

If filename has no extension load looks for filename.mat and, if found, load treats the file as a binary ‘MAT-file’. 

If filename.mat is not found, or if filename has an extension other than .mat, it’s treated as an ASCII file.

Type help load on your command window to see the whole description.

Now, we’re going to develop a routine like this...
 

% clear memory, screen and avoid blank lines
clear all, clc, format compact 

% merge text files
load results.txt 

% work with the whole matrix
fr = floor(results) 

% read columns 1 and 3
x = results(:, 1)
z = results(:, 3) 

% read rows 1 and 4
row1 = results(1, :)
row4 = results(4, :)

 

We get the following:

fr =
    13    58    20    41
     1    42    37    30
    89    51    78    87
    19    33    68     1
    29    43    46    76
    66    22    56    97
    28    57    79    99

x =
   13.6519
    1.1757
   89.3898
   19.9138
   29.8723
   66.1443
   28.4409

z =
   20.9069
   37.9818
   78.3329
   68.0846
   46.1095
   56.7829
   79.4211

row1 =
   13.6519   58.2792   20.9069   41.5375

row4 =
   19.9138   33.3951   68.0846    1.5009
 

We continue our script...

% generate some new numbers
a = x + z;
b = ceil(2*x - z/5);
c = row1 + row4; 

% see variables in the workspace
whos
 

And the variables so far have these properties...
 

Name          Size                Bytes  Class 

 a             7x1                   56  double array
 b             7x1                   56  double array
 c             1x4                   32  double array
 fr            7x4                  224  double array
 results       7x4                  224  double array
 row1          1x4                   32  double array
 row4          1x4                   32  double array
 x             7x1                   56  double array
 z             7x1                   56  double array 

Grand total is 96 elements using 768 bytes 

 
We continue our script by saving a file (this time it’s a mat-file) with only three variables...
 

Another useful command is 'save', which saves workspace variables to disk. save filename saves all workspace variables to the binary ‘MAT-file’ named filename.mat. The data may be retrieved with load. If filename has no extension, .mat is assumed.  save filename X saves only X.

Type help save on your command window to see the whole description.

 

% save .mat file only with variables a, b and c
save output_data a b c 

% remove all variables, globals, functions
clear all 

%see updated workspace (expected empty)
who 

% load .mat file
load output_data 

% see workspace
who 

% see what you loaded
a
c
 

The Matlab command window shows finally 

Your variables are: 

a  b  c  

a =
   34.5588
   39.1575
  167.7227
   87.9984
   75.9818
  122.9272
  107.8620
c =
   33.5657   91.6743   88.9915   43.0384


 From 'Merge Text Files' to home

 From 'Mege Text Files' to Matlab Tutorial
 
Top

Read a sequence of text files

ASCII chart




footer for merge text files page