Delimited Files

In [1]:
A = rand(50, 5);
% the simplest form
% folder data has to exist
dlmwrite('data/random_data.txt', A);
% really, anything can be an identifier
dlmwrite('data/random_data.txt', A, 'delimiter', '#', 'precision', 3);

Let's preview the file (just for demonstration). DO NOT use fileread for numeric data reading. Also, use a text editor or Excel for previewing files.

In [2]:
% to read data as string, not that useful
% I'm using it to just preview the file
fileread('data/random_data.txt')
ans = 0.955#0.454#0.679#0.00193#0.316
0.237#0.217#0.204#0.0509#0.867
0.624#0.851#0.343#0.739#0.0568
0.384#0.234#0.101#0.0752#0.196
0.287#0.908#0.958#0.21#0.493
0.629#0.414#0.0113#0.996#0.311
0.0583#0.225#0.59#0.657#0.686
0.472#0.0553#0.742#0.475#0.319
0.631#0.679#0.414#0.356#0.902
0.375#0.356#0.559#0.735#0.052
0.0424#0.512#0.734#0.434#0.887
0.396#0.478#0.325#0.638#0.124
0.952#0.415#0.648#0.929#0.61
0.809#0.941#0.0101#0.268#0.988
0.626#0.401#0.935#0.729#0.59
0.0799#0.465#0.611#0.101#0.665
0.242#0.266#0.733#0.996#0.655
0.613#0.113#0.0604#0.864#0.672
0.251#0.102#0.797#0.246#0.972
0.295#0.792#0.15#0.148#0.93
0.488#0.786#0.125#0.119#0.572
0.932#0.84#0.52#0.81#0.919
0.135#0.945#0.506#0.764#0.694
0.883#0.112#0.492#0.459#0.55
0.364#0.196#0.106#0.13#0.512
0.624#0.236#0.414#0.0125#0.413
0.299#0.655#0.715#0.731#0.63
0.522#0.595#0.268#0.0952#0.332
0.229#0.141#0.348#0.181#0.408
0.351#0.204#0.0275#0.104#0.264
0.494#0.427#0.98#0.125#0.637
0.0324#0.484#0.893#0.926#0.723
0.627#0.0344#0.473#0.138#0.713
0.37#0.367#0.85#0.854#0.179
0.882#0.602#0.126#0.844#0.693
0.883#0.995#0.839#0.459#0.177
0.436#0.484#0.987#0.00428#0.441
0.516#0.735#0.597#0.516#0.601
0.838#0.114#0.548#0.337#0.443
0.647#0.926#0.141#0.0755#0.998
0.677#0.237#0.585#0.762#0.478
0.659#0.143#0.0498#0.0103#0.957
0.0157#0.037#0.987#0.761#0.351
0.711#0.499#0.875#0.22#0.387
0.0713#0.28#0.551#0.981#0.473
0.534#0.548#0.927#0.465#0.376
0.458#0.828#0.559#0.756#0.0115
0.865#0.126#0.748#0.48#0.765
0.667#0.0871#0.827#0.927#0.726
0.799#0.333#0.586#0.719#0.533

Reading the file in.

In [3]:
% overwrite and use a normal delimiter (tab, comma, space, semicolon)
dlmwrite('data/random_data.txt', A, '\t');
% normal delimiter will be detected automatically, but we can explicit
B = dlmread('data/random_data.txt', '\t')
B =

   0.9552013   0.4544261   0.6794266   0.0019308   0.3160475
   0.2367287   0.2165404   0.2042917   0.0508895   0.8667649
   0.6237217   0.8512350   0.3431843   0.7393250   0.0568058
   0.3839312   0.2341675   0.1013015   0.0752194   0.1958978
   0.2869135   0.9082074   0.9583929   0.2103591   0.4934745
   0.6285677   0.4144439   0.0112582   0.9961881   0.3107998
   0.0582971   0.2251855   0.5896687   0.6567541   0.6855137
   0.4721780   0.0553121   0.7417446   0.4749404   0.3186908
   0.6306129   0.6792960   0.4135072   0.3564722   0.9017628
   0.3753582   0.3558965   0.5593777   0.7350293   0.0520043
   0.0424203   0.5122452   0.7341369   0.4341257   0.8868061
   0.3964040   0.4782519   0.3247449   0.6375229   0.1235749
   0.9517365   0.4150440   0.6480372   0.9288534   0.6098179
   0.8093328   0.9410099   0.0101180   0.2678137   0.9879661
   0.6261404   0.4014659   0.9346491   0.7293849   0.5900712
   0.0798854   0.4649516   0.6108473   0.1006724   0.6648730
   0.2421774   0.2663891   0.7327463   0.9961236   0.6551711
   0.6132360   0.1126744   0.0604030   0.8642149   0.6721689
   0.2511185   0.1018348   0.7972716   0.2462516   0.9719434
   0.2945736   0.7920001   0.1495971   0.1477662   0.9295946
   0.4880764   0.7863818   0.1245801   0.1186490   0.5718633
   0.9317301   0.8395929   0.5204686   0.8101369   0.9191383
   0.1347754   0.9445392   0.5057554   0.7637987   0.6938555
   0.8828731   0.1121944   0.4924511   0.4585065   0.5498818
   0.3638550   0.1959555   0.1055363   0.1302887   0.5119128
   0.6244901   0.2357458   0.4141716   0.0124632   0.4128813
   0.2992359   0.6548002   0.7153125   0.7314469   0.6301484
   0.5221161   0.5950118   0.2680238   0.0951706   0.3321104
   0.2290549   0.1405481   0.3483681   0.1809435   0.4076978
   0.3508465   0.2041402   0.0274793   0.1035787   0.2637067
   0.4937128   0.4273886   0.9799113   0.1245732   0.6366661
   0.0323586   0.4839067   0.8934720   0.9259389   0.7232716
   0.6274219   0.0343534   0.4726747   0.1375234   0.7125470
   0.3700322   0.3668366   0.8501808   0.8541019   0.1794761
   0.8822750   0.6021070   0.1256313   0.8435429   0.6926763
   0.8834667   0.9952884   0.8394516   0.4585563   0.1767298
   0.4363256   0.4839176   0.9874950   0.0042763   0.4413101
   0.5156494   0.7345180   0.5970792   0.5164172   0.6013013
   0.8378527   0.1136031   0.5480995   0.3369989   0.4432172
   0.6473346   0.9261179   0.1414082   0.0754611   0.9977774
   0.6774932   0.2374364   0.5847188   0.7615071   0.4783514
   0.6587631   0.1429923   0.0497609   0.0103341   0.9567853
   0.0157098   0.0370116   0.9872839   0.7610383   0.3512414
   0.7113933   0.4989311   0.8751197   0.2195630   0.3866395
   0.0712719   0.2799013   0.5511715   0.9807097   0.4727254
   0.5339084   0.5484494   0.9274099   0.4645626   0.3764179
   0.4580829   0.8275270   0.5590040   0.7561167   0.0114561
   0.8651764   0.1259940   0.7475218   0.4801367   0.7653277
   0.6666689   0.0871092   0.8265874   0.9267221   0.7259552
   0.7993271   0.3334294   0.5860477   0.7190202   0.5329332

$\texttt{dlmread}$ will work with CSVs if they're only numbers

In [4]:
% CSVs typically contain the first line describing the data with strings
fileread('data/example.csv')
ans = Week_nb, Temperature, Cloud_coverage, Rain
1, 23, 70, 0
2, 18, 100, 1
5, 17, 100, 1
7, 17, 100, 0
12, 19, 70, 0
13, 21, 50, 0
14, 22, 10, 0

In [5]:
% dlmread will ignore text by using empty numbers (zeros)
% notice that the first row is just zeros instead of text
data = dlmread('data/example.csv', ',')
data =

     0     0     0     0
     1    23    70     0
     2    18   100     1
     5    17   100     1
     7    17   100     0
    12    19    70     0
    13    21    50     0
    14    22    10     0

Custom Writing/Reading

Using $\texttt{fprintf}$ to write formatted text to file.

In [6]:
% folder data has to exist
fid = fopen('data/custom_written.txt', 'w');

% reference to the file, doesn't mean anything, but identifies the file
fid 

fprintf(fid, 'The day is %s\n', 'Thursday');
fclose(fid); % don't forget to close the file
fid =  3
In [7]:
fileread('data/custom_written.txt')
ans = The day is Thursday

Using $\texttt{textscan}$ to read custom text.

In [8]:
fileread('data/custom_text.txt')
ans = String 42 23.0 c
Name 2 -3.0 f
Word -22312 17.0 h
Characters 00234 Inf z

In [9]:
fid = fopen('data/custom_text.txt', 'r');
% use formatters
% %s - string, character
% %d - integer
% %f - floating point number (real number)
data = textscan(fid, '%s %d %f %s')

% if you opened a file, you have to close it
fclose(fid);
data =
{
  [1,1] =
  {
    [1,1] = String
    [2,1] = Name
    [3,1] = Word
    [4,1] = Characters
  }

  [1,2] =

        42
         2
    -22312
       234

  [1,3] =

      23
      -3
      17
     Inf

  [1,4] =
  {
    [1,1] = c
    [2,1] = f
    [3,1] = h
    [4,1] = z
  }

}

In [10]:
% data is organized into a cell array of columns

% strings are given in a cell array
data{1}
ans =
{
  [1,1] = String
  [2,1] = Name
  [3,1] = Word
  [4,1] = Characters
}

In [11]:
% numbers are given in a vector
data{2}
ans =

      42
       2
  -22312
     234