HOW TO READ DATA FROM 4 TEXT FILES SIMULTANEOUSLY LINE BY LINE AND FIND THE MEDIAN OF EACH LINE ?

3 views (last 30 days)
i have got 4 .txt files which represents the data at 4 different times in a day, which iam attaching below. And i have to write a program such that it start reading from 17th line(after the heading pressure) in all the 4 files simulatenously. The secondlast column in all the file specifies the height and i have to find its median. so what i finally get should be the median of the height(second last colmn in each file) These 4 files should be calculated together. (For example : when these 4 files are placed sidebyside, we get 4 height data on the 17th line itself. find the median of these 4 data ..like that)
This process has to be repeated for the entire lines of the file.So finally we get about more than 500 median value. Can u pls help me write a code for this task? Iam new to matlab and have very little programming skill. ANY HELP WOULD BE MUCH APPRECIATED.PLS

Accepted Answer

Rik
Rik on 18 Jan 2019
This code should do the trick:
fnames={'1OCT3AM.txt','1OCT6AM.txt','1OCT9AM.txt','1OCT12AM.txt'};
lastcol=zeros(1,numel(fnames));
n_lines_skip=16;
FormatSpec='%d %d %d %d %d %d %d %d %f %f %f %f %f';
for n_file=1:numel(fnames)
fid=fopen(fnames{n_file},'r');
c=textscan(fid,FormatSpec,'HeaderLines',n_lines_skip);
lastcol(1:numel(c{end}),n_file)=c{end};
fclose(fid);
end
median_values=median(lastcol,2);
  6 Comments
lakshmi dwaraka
lakshmi dwaraka on 18 Jan 2019
Edited: lakshmi dwaraka on 18 Jan 2019
The loop i gave seems to overwrite the median_values. so only the last iteration value is getting as output.
Thanks for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!