How to combine multiple .txt and calculate Mean, Std, Min, Max

I have several .txt files like this
A .txt=
10 1 4
20 2 5
30 3 6
B.txt
10 7 11
20 8 12
30 9 13
C..txt
10 13 18
20 14 19
30 15 20
D.txt
10 1 4 7 11 13 18
20 2 5 8 12 14 19
30 3 6 9 13 15 20
I want to combine those file into once like in D.txt and plotting the files as first column is y ordinat [10,20,30] and others is x absis.
Then I want to calculate, mean , median, std and get max an min values of my file.
The Result like this
Mean Std Min Max
Y ordinat X absis
10 1 4 7 11 13 18 5 5.883795 -0.8838 10.8838
20 2 5 8 12 14 19 5 5.992058 -0.99206 10.99206
30 3 6 9 13 15 20 5 6.12178 -1.12178 11.12178
Any one can help? I would be appreciate.
Thanks you
Haifani

5 comentarios

Are you just plugging in random values for mean, std, min, max in your table above? Or how are you planning on calculating them? the max of each row should be 18, 19, 20 respectively.
Skydriver
Skydriver el 30 de Oct. de 2019
Editada: Skydriver el 30 de Oct. de 2019
I calculate mean, std with excel and I want to compute in Matlab. The 18 19 and 20 is the values of C.txt in row 3. D,txt is merge files of A.txt, B.txt and C.txt.
Can you explain how you get 10.8838, 10.99206 or 11.12178 as your maxima?
Ah, ok I see. min and max are the range of your solution from the mean by one standard deviation!
Skydriver
Skydriver el 30 de Oct. de 2019
Editada: Skydriver el 30 de Oct. de 2019
Yes. Off course So for developed the figure I only use one column of each as the y ordinat. Yes I also explained about standard deviation. But I still missing how to merge the data as displat in D.txt.

Iniciar sesión para comentar.

 Respuesta aceptada

Turlough Hughes
Turlough Hughes el 30 de Oct. de 2019
Editada: Turlough Hughes el 30 de Oct. de 2019
Put all your txt files in a folder and you can then use the following
myfolder='name of the folder'
fils=dir([myfolder '\*.txt']); % lists all the files with a .txt extension in myfolder
[~,idx]=sort({fils.name}); % index to load .txt files alphabetically
temp=readmatrix([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Y=temp(:,1); X=temp(:,2:end);
for ii=idx(2:end)
temp=readmatrix([fils(ii).folder '\' fils(ii).name]);
X=[X temp(:,2:end)];
end
plot(X,Y)
As for your statistics you may need to modify mean and std to suit your needs:
meanD=mean(X,2); % these are the mean values of each row...
stdD=std(X,0,2); % standard deviations of each row.
medianD=median(X,2);
minD=meanD-stdD;
maxD=meanD+stdD;
D=table(Y,X,meanD,stdD,medianD,minD,maxD)
writetable(D,'D.txt')
D.txt will be saved in your working directory.

5 comentarios

Skydriver
Skydriver el 30 de Oct. de 2019
Editada: Skydriver el 30 de Oct. de 2019
Thanks, but I have small problem. When I running with your code, I get small mistake like this:
My coding will be like this:
File={'A.txt','B.txt','C.txt}
or
A=[10 1 4;20 2 5;30 3 6];
B=[10 7 11;20 8 12;30 9 13];
C=[10 13 18;20 14 19;30 15 20];
File={A,B,C};
myfolder='File'
fils=dir([myfolder '\*.txt']); % lists all the files with a .txt extension in myfolder
[~,idx]=sort({fils.name}); % index to load .txt files alphabetically
temp=readmatrix([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Y=temp(:,1); X=temp(:,2:end);
for ii=idx(2:end)
temp=readmatrix([fils(ii).folder '\' fils(ii).name]);
X=[X temp(:,2:end)];
end
plot(X,Y)
I got a massage:
Index exceeds matrix dimensions.
temp=readmatrix([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Turlough Hughes
Turlough Hughes el 30 de Oct. de 2019
Editada: Turlough Hughes el 30 de Oct. de 2019
You need include the full pathname in the variable myfolder, i.e.
myfolder='C:\...\File'
So, in this case you would have your text files A,B,C, etc., located in a folder called File.
Do you have another option to read the file?
I try with this :File={'A.txt','B.txt','C.txt}
Because with this code give a massage:
Reference to non-existent field 'folder'.
Error in CalcAll (line 50)
temp=readmatrix([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Yea, I wrote the above to allow flexibility in the number of files being loaded in, but you can also place the files in your working directory and load them in as follows:
A=readmatrix('A.txt');
B=readmatrix('B.txt');
C=readmatrix('C.txt');
X=[A(:,2:end) B(:,2:end) C(:,2:end)];
Y=A(:,1);
plot(X,Y)
Skydriver
Skydriver el 31 de Oct. de 2019
Editada: Skydriver el 31 de Oct. de 2019
Nice its works know I change readmatrix with importdata. I use Matab2018a.
Thank you
Akhmad

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 30 de Oct. de 2019

Editada:

el 31 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by