I am working in a observation and the data is saved in .txt. I have 111 folders which each of them consists of 816 .txt files and this is how the data looks like.
And this is the script I created to read all files in a folder.
dir_in = 'D:\DATA 1\Materi & Tugas Kuliah\Tugas Akhir\c2\a\c020_out\';
files = dir([dir_in, '\*.txt']);
filename = (files.name);
fid = fopen(filename, 'rt');
c = textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s');
lat = c{4};
lon = c{5};
SSH = c{16};
SLA = c{17};
MJD = c{3};
YYF = c{19};
fclose(fid);
But everytime I run the script, the data does not appear. Textscan on variable 'c' reads the first line of the files ONLY, which is the header line, either using 'headerlines' option or not. I guess something wrong goes in that 'fid', but I am not really sure.
I hope someone can help me out. Thank you!

 Accepted Answer

Walter Roberson
Walter Roberson on 23 Mar 2019
Edited: Walter Roberson on 23 Mar 2019
filenames = fullfile(dir_in, {files.name});
numfiles = length(filenames);
lat = cell(numfiles,1);
lon = cell(numfiles,1);
SSH = cell(numfiles,1);
SLA = cell(numfiles,1);
MJD = cell(numfiles,1);
YYF = cell(numfiles,1);
numfields = 19; %adjust as needed
for K = 1 : numfiles
fid = fopen(filenames{K}, 'rt');
c = textscan(fid, repmat('%f', 1, numfields), 'Headerlines', 1);
lat{K} = c{4};
lon{K} = c{5};
SSH{K} = c{16};
SLA{K} = c{17};
MJD{K} = c{3};
YYF{K} = C{19};
fclose(fid);
end

1 Comment

omg that works! thank you very much for your help!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!