user3137771
user3137771

Reputation: 11

How do i read text file using command textscan

I have a rather large text file (over 2050 lines x 4080 columns) that has the following format:

      #1     #2       #3...........#10        #1       #2      #3......
Time 21:22:10 21:23:56 21:23:07....06:19:11 06:21:00 06:21:23 06:23:01......
15   0.00     0.00      0.00   .... 0.00    0.00     0.00     0.00......
30    -6.09    1200.44  32.08   .... -0.17    9.87     -44.65    768.12......
.      .         .        .     .....       .         .         .........
.      .         .        .     .....       .         .         .........
.      .         .        .     .....       .         .         .........
2050   76.009   32.98  -5.91    .....        15.54   -87.60    -10.74 ......

How do I read text file using command textscan in MATLAB?

Upvotes: 0

Views: 133

Answers (2)

user3137771
user3137771

Reputation: 11

it's not that really,i get that vectors in array cell.I can to use this:

PathName = uigetdir;
d = dir(fullfile(PathName,'*.txt'));
for i = 1:numel(d)
   A{i}= dlmread(fullfile(PathName, d(i).name),'',2,1); 
   B{i}= textscan(fid, '%s', 2, 'delimiter', '\n', 'headerlines', 0);
end

Upvotes: 0

Floris
Floris

Reputation: 46375

Given that the first two rows seem to be different than the others, I would recommend doing three read operations: first line, second line, "everything else". Do you actually want the contents of the first line (not clear from your question)? Second line should probably be read as a series of strings, to be converted to time using datevalue function. The rest can be read with a simple "%f", Inf formatting statement.

This means it will look something like this (can't test right now):

fid = fopen('myfile.dat', 'r');
b = textscan(fid, '%s', 4080, 'headerLines', 1);
c = textscan(fid, '%f');
fclose(fid);

Then you will have one time stamp in each cell of b (with 'Time' in cell b{1}), and an array of all the values in the first cell of c. You could further convert these with:

times = b{1}(2:end); % get rid of the 'Time' string
timeStamps = cellfun(@(x)datenum(x), times); % convert string to "date" numbers

To get the values in the right order (rows/colums as in the file) you need to transpose (since matlab stores numbers row-first):

values = reshape(c{1}, 4080, [])';

Upvotes: 1

Related Questions