How to calculate average value in each row of data (Not a matrix) ?

4 views (last 30 days)
Hello ^^
I have a lot of different file like this
File 1 : j1p001.txt
90 1.456 0.998 0.114 NaN 1.002 1.987 ..
100 0.411 1.223 0.01 NaN 0.201 .. .. ..
-98 1.446 0.998 0.023 NaN 3.244 0.88 .. ..
-89 1.098 3.113 1.01 NaN 0.234 0.01 NaN 0.234
..
..
File 2 : j1p002.txt
And other file have the same structure, but with different number of rows and columns.
I want to calculate the mean value in each row (but it start at column 2 to end of the column) and save it in new file.
i just code something but it still error
lc;
clear;
format short;
Y = dir('D\Reformat new file\done\j1p*.txt');
for A=1:length(Y);
H = Y(A).name; gg = H(:,1:7);
data=load(H);
ref=data(:,1);
index_data=find(~isnan(data));
data_fix=data(index_data,1);
n=length(data);
out=mean(data_fix,2);
result=[ref out];
file_name = [gg '_result.txt'];
dlmwrite(file_name,result,'delimiter','\t');
end
Thank you
  6 Comments

Sign in to comment.

Accepted Answer

Stjepan Mamusa
Stjepan Mamusa on 14 Sep 2021
Edited: Stjepan Mamusa on 14 Sep 2021
Here's my proposal. First read all the files and separate good from bad. Then, do the work on healthy files.
Click the green arrow to test it out in place ( you can copy - paste the code in a new answer and run it there, but it's better to copy it and run it locally on your files ).
The file j1p404.txt has errors created by me, to test if error detection will work.
UPDATE: Edit with @Rik's suggestion to use omitnan flag, it will give better results, as my solution meant the NaN's are zeroes actually.
clear;
clc;
format short;
file_list = dir('j1p*.txt');
ok_files = dir('');
nok_files = dir('');
% Check file list for errors in files
for i = 1 : length( file_list )
try
tmp = load( file_list(i).name );
ok_files(i) = file_list(i);
catch
% In case of error remove the file from list
fprintf('Error occured with file %s\n',file_list(i).name );
nok_files(i) = file_list(i);
end
clear tmp;
end
Error occured with file j1p404.txt
file = fopen('ok_files.txt','w');
fprintf(file,'%s\n', ok_files.name);
fclose(file);
file = fopen('nok_files.txt','w');
fprintf(file,'%s\n', nok_files.name);
fclose(file);
clear i file nok_files;
for i = 1 : length( ok_files )
data = load( ok_files(i).name );
averaged = zeros( size(data,1), 2);
averaged(:,1) = data(:,1);
for j = 1 : size(data,1)
averaged(j, 2) = mean( data(j, 2:end), 'omitnan' );
end
file = fopen(strcat('averages_', ok_files(i).name ),'w');
fprintf(file,'%12s %12s\n','My Variable','Average');
fprintf(file,'%12.6f %12.6f\n', averaged' );
fclose(file);
disp( ok_files(i).name );
disp(averaged);
end
j1p001.txt
90.0000 1.1114 100.0000 0.4613 -98.0000 1.3182 -89.0000 1.0930
j1p002.txt
90.0000 1.1114 100.0000 0.6602 -98.0000 1.3023 -89.0000 1.0930
j1p003.txt
90.0000 1.3892 100.0000 1.0178 -98.0000 1.4896 -89.0000 1.0792
This will create a list of healthy (ok) files and, a list of files where you had issue with measurement. It will calculate all of your row averages and save them in a separate file.
I have attached all the files I used.
I took the liberty to create some dummy data in the form of j1p*.txt files, all the rest except script.m are generated by the script.
If this solved your problem, please mark the question as answered.
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by