Clear Filters
Clear Filters

select data from a text file

1 view (last 30 days)
UWM
UWM on 16 Mar 2016
Commented: UWM on 16 Mar 2016
Matlab users,
I have a question regarding selecting certain data from a text file. First of all I need to find all rows where the values in first and second columns are the same (measurement epoch). Then I have to calculate the arithmetic mean of the values in fourth, fifth and sixth columns (for the identified “the same” rows). Finally I want to save in the output file “age of observation: values from first and second column" and corresponding to them arithmetic mean. Below there is attached a small piece of the input file.
Does anyone have any suggestions on how I might be able to do this?
Many thanks!
0 50 1 -0.20 0.92 -1.95
0 55 1 -0.20 0.94 -1.97
1 0 1 -0.21 0.95 -2.00
1 5 1 -0.33 0.66 -1.86
1 10 1 -0.33 0.68 -1.88
1 15 1 -0.38 0.66 -1.98
1 20 1 -0.38 0.67 -2.00
1 25 1 -0.44 0.65 -2.13
1 30 1 -0.44 0.25 -1.51
1 35 1 -0.44 0.26 -1.52
1 40 1 -0.48 0.30 -1.69
1 45 1 -0.48 0.30 -1.70
1 50 1 -0.51 0.31 -1.79
1 55 1 -0.51 0.31 -1.79
2 0 1 -0.57 0.32 -1.96
2 5 1 -0.57 0.32 -1.95
2 10 1 -0.62 0.30 -2.04
2 15 1 -0.62 0.29 -2.03
1 50 2 -0.22 1.33 -2.61
1 55 2 -0.22 1.34 -2.64
2 0 2 -0.35 1.23 -2.81
2 5 2 -0.28 1.15 -2.50
2 10 2 -0.28 1.16 -2.52
2 15 2 -0.23 1.11 -2.29
2 20 2 -0.28 0.74 -1.85
2 25 2 -0.28 0.75 -1.86
2 30 2 -0.28 0.71 -1.81
2 35 2 -0.28 0.72 -1.82
2 40 2 -0.29 0.70 -1.83
2 45 2 -0.29 0.71 -1.83
2 50 2 -0.33 0.69 -1.91

Accepted Answer

Guillaume
Guillaume on 16 Mar 2016
Reading the file is easy with dlmread or readtable.
For calculating the means of identical epoch, you can use the Split-Apply-Combine workflow introduced in R2015b, or unique with accumarray:
[epoch, ~, subs] = unique(m(:, [1 2]), 'rows'); %find unique epochs as rows of column 1 and 2
meancol5 = accumarray(subs, m(:, 5), [], @mean); %mean of column 5 by epoch
  1 Comment
UWM
UWM on 16 Mar 2016
Thank you for help. It works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays 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!