MATLAB: Averaging sections of a textfile

acumarray

I want to average specific sections of a complex text file for data analysis. All files that are to be analysed are the same format so I want to automate the process. I have previously asked a question on this forum but the solution provided only work for the simpler text files I am analysing.
I want to average the data by frequency value, so:
Freq Force etc.
5 654 ....
5 754 ....
5 645 ....
10 1029 ....
10 1175 ....
10 1110 ....
15 1354 ....
15 1375 ....
15 1390 ....
Becomes:
Freq Force etc.
5 684 ....
10 1105 ....
15 1373 ....
My current code, produced with the help of Stephen Cobeldick from a previous question.
fid = fopen('text1.txt','rt');
cal = readtable('text1.txt');
fclose(fid);
tabledata = readtable('text1.txt');
tabledata(:,1:4)=[];
arraydata = table2array(tabledata);
arraydata = sortrows(arraydata);
Freq = unique(arraydata(:,1))
A = accumarray(Freq,arraydata(:,2:12),[5 15],@mean)
Currently, gives the error 'Second input VAL must be a vector with one element for each row in SUBS, or a scalar.' I have attached the text file being analysed.

Best Answer

tables have all sort of built-in functions to do what you want. No need to convert the table back to an array or to use accumarray. In your particular case, varfun will do the job easily and simply:
cal = readtable('text1.txt');
meancal = varfun(@mean, cal, 'GroupingVariables', 'Freq')
That's all that is needed.
Note: reading the documentation of matlab, and looking at the examples is a good way to learn how to do things. Trying things at random, without thinking it through as you appear to have done (see Stephen's comment) is not.