load multiple .dat file and do the same calculation for same columns and make an output file

2 次查看(过去 30 天)
Hello All and let me thank you for the time you spend to read my question I have seventy .dat file(s1.dat to s70.dat) each include 8columns and 6000rows, I want to find the maximum for column 5 and 6 for each .dat file and make a new file with 70rows and 2 columns(like following)
1 C5max C6max
2 C5max C6max
.
.
70 C5max C6max
I will appreciate every single tips or advice
  3 个评论
Samaneh Arzpeima
Samaneh Arzpeima 2018-4-16
thank you Sir. it would be better if I mentioned at the begining that I am a beginer. I went trough your suggested link try to use the codes,but didn't work for me.

请先登录,再进行评论。

回答(1 个)

Etsuo Maeda
Etsuo Maeda 2018-4-17

If your sXX.dat is a csv file, the following code will help you to understand.

% generate 7 test files
for k = 1:7
    T = randi(100, 6000, 8);
    filename = ['csv', num2str(k), '.dat'];
    csvwrite(filename, T);
end
clear; % clear variables
% load csvfiles, find maximum, store values
for k = 1:7
    filename = ['csv', num2str(k), '.dat'];
    S = csvread(filename);
    M_col5 = max(S(:, 5));
    M_col6 = max(S(:, 6));
    M{k, :} = [M_col5, M_col6];
end
% save results
csvwrite('results.dat', M)
  1 个评论
Samaneh Arzpeima
Samaneh Arzpeima 2018-4-18
编辑:Walter Roberson 2018-4-18
Yours Example was super easy to understand.Thank you very much. Unfortunetaly My file is dat file with space, and I want to jump the first 22nd row and just take the 5th and 6th colomns.and find the max of each and extract to a new file.I temp a part of one of my file(I have 77of them, site1.dat ...site71.dat) could you please an other advice.sorry to ask for very elementry question. below was the only cods that came to my mind clear; clc;
datafiles = dir('*.dat');
for k = 1:71
filename = datafiles(k).name;
startRow = 22;
formatSpec = '%*60s%15f%15f%[^\n\r]';
fileID = fopen(filename,'r');
fclose(fileID);
delimiterIn= ' ';
headerlinesIn = 22;
S = importdata(filename,delimiterIn,headerlinesIn);
M_col5 = max(S(:, 5 ));
M_col6 = max(S(:, 6 ));
M{k, :} = [M_col5, M_col6 ];
end%

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by