I have a piece of code that reads a .txt file with several matrices and sorts them into cell arrays. Each of the matrices has always three columns with varying rows. My question would be how i can take the average of each column from these matrices separately and put it into another matrix with these averages, so that it would be a new matrix containing 3 columns, each column with the average of the column of the original matrix, and with the number of rows like the number of matrices on the .txt file. Here the code that reads the .txt file and indexed is the .txt file.
filepath = '/Users/...';fileID = fopen(filepath);nGroups = 0;stationGroups = cell(0,1);while true nStations = fgets(fileID); if ~ischar(nStations) break; end nGroups = nGroups + 1; nStations = str2num(nStations); stationMatrix = zeros(nStations,3); for currentStation = 1:nStations fprintf('Loading station %d of station group %d: ', ... currentStation, nGroups); testStation = fgets(fileID); if ~ischar(testStation) error('Error - file ended too early!'); end testStation = str2num(testStation); if numel(testStation)~=3 error('Error - Station %d didn''t have three values!', ... currentStation); end fprintf('%.2f ',testStation); fprintf('\n'); stationMatrix(currentStation,:) = testStation; end stationGroups{end+1} = stationMatrix; nGroups = length(stationGroups);endfclose(fileID);
Best Answer