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

조회 수: 3 (최근 30일)
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
Rik
Rik 2021년 9월 14일
Based on your last edit, I would say you have found out how to calculate the mean. You might be interested in the 'omitnan' flag.
Also, arrays in Matlab always have the same number of columns for each row, so I don't know what you mean with your last remark.
Stjepan Mamusa
Stjepan Mamusa 2021년 9월 15일
Hi @Retno Purwaningsih , can you verify if the answer I gave is working for you

댓글을 달려면 로그인하십시오.

채택된 답변

Stjepan Mamusa
Stjepan Mamusa 2021년 9월 14일
편집: Stjepan Mamusa 2021년 9월 14일
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
Stjepan Mamusa
Stjepan Mamusa 2021년 10월 4일
nok_files is what I called files that are, well, not okay in some way. Those are mainly files that have some issues in formatting or missing values, so the best thing would be to fill in those missing values with NaN variables.
Retno Purwaningsih
Retno Purwaningsih 2021년 10월 20일
Could you help me to fill those missing value?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by