How to calculate average value in each row of data (Not a matrix) ?
1 view (last 30 days)
Show older comments
Retno Purwaningsih
on 14 Sep 2021
Commented: Retno Purwaningsih
on 20 Oct 2021
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
Accepted Answer
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
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
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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!