MATLAB Answers

0

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

Asked by lakshmi dwaraka on 18 Jan 2019
Latest activity Commented on by Rik Wisselink on 18 Jan 2019
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

  0 Comments

Sign in to comment.

1 Answer

Answer by Rik Wisselink on 18 Jan 2019
 Accepted Answer

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

The modification you are asking for are really basic operations. Why don't you try to learn using the tool you are working with? Any tutorial should teach you how to do this.
The loop i gave seems to overwrite the median_values. so only the last iteration value is getting as output.
Thanks for the help!
Glad to be of help. If my answer solved your question, please click the 'accept answer' button.

Sign in to comment.