Sunday, 4 August 2013

Read data from txt file in matlab

Three different types of data are listed here:
1. data with nice format
time90.ipt
------------------content-----------------

# output         -- if =1 the result will only be out
#   [Start time]          [End Time]      [Comment on the data]  [Water level cm]
 2012/Dec/03 11:54, 2012/Dec/07 10:00,   ET not complete TunnelET5(W),  0,  5,1,  0,1,  60,  0,   0
 2012/Dec/07 11:24, 2012/Dec/12 12:02,   ET not complete  NA(W2),       0, 4, 1,   0,    2, 60, 0, 0
 2012/Dec/12 18:00, 2012/Dec/31 17:33,   Missing mois data PET6.5(M),   0, 3,  1,   0,   3,60,0, 0

--------------end of content----------------------
Since there is only one table, and each column of the content has been separated by delimiter one can use
>>time=importdata('time90.ipt');


2. multi format data without delimiter:
EN.IPT
-----------------------contnet-------------------------

# MXC-- THE MAXIMUM VALUE FOR CONCENTRATION OUTPUT
# MXS-- THE MAXIMUM VALUE FOR SOLID SALT OUTPUT
# MXD-- IN THE EVAPORATION GRAPH, THE MAXIMIZED


# MXE-- IN THE EVAPORATION GRAPH,
[FNAME]  [DI]      [FILENAME1]          [FILENAME2]         [FILENAME3]
7C90MM     2D       SUTRA5.7_6_PET8_M2     SUTRA5.7_4_PET10_M      
#[SWELE]  [SWNOD]   [SWBCOP]    [SWBCOF]   [INTERP]
   71     72        0           71       0              20          0       200000       0        0.06
--------------end of content----------------------
only the line without hatch is useful then one can use

fn=fopen('EN.IPT');
line=fgetl(fn);
while  line(1)=='#'
  line=fgetl(fn);
end
line=fgetl(fn);
f1= textscan(line,'%s');

line=fgetl(fn);
f2=fscanf(fn,['%f' ' '],[1,10]);

in this case, file is read line by line.


3. multi format data with delimiter, especially for CSV file:






No comments:

Post a Comment