Daniel@0: function [CovMatrix, obs, varfields] = CovMat(filename,row_cols) Daniel@0: %[CovMatrix, obs, varfields] = CovMat(filename,row_cols) Daniel@0: %% generates a Covariance Matrix from a file of data consisting of N columns of M data rows Daniel@0: %% filename string name (with path and extension) of file to open Daniel@0: %% row_cols Number_of_converstions_per_row (turns into [3 inf]) Daniel@0: %% Return Daniel@0: %% CovMatrix Covariance matrix Daniel@0: %% obs Number of observations read in Daniel@0: %% varfields Labels of the variables see filename structure below Daniel@0: %% Daniel@0: %% Filename structure: Daniel@0: %% Comma separated, starting with the variable labels, then the data in rows. Daniel@0: %% filename test.txt consists of: Daniel@0: %% Daniel@0: %% Earthquake,Burglar,Radio,Alarm,Call Daniel@0: %% 1,2,3,4,5 Daniel@0: %% 11,22,33,44,55 Daniel@0: %% . . . Daniel@0: %% Daniel@0: %% Example call: Daniel@0: %% [cvmat numdat lables] = CovMat('test.txt',5); Daniel@0: %% Daniel@0: %% Returns Covariance matrix, number of date rows and variable field names Daniel@0: %% Gary R. Bradski 7/2002 Daniel@0: Daniel@0: fmtstr = '%f'; Daniel@0: for i = 2:row_cols Daniel@0: fmtstr = strcat(fmtstr,',%f'); Daniel@0: end Daniel@0: Daniel@0: %% load data Daniel@0: fidCov = fopen(filename,'r'); Daniel@0: Daniel@0: varfields = fgetl(fidCov); Daniel@0: Corx = fscanf(fidCov,fmtstr,[row_cols inf]); Daniel@0: Corx= Corx'; Daniel@0: [obs bla] = size(Corx); Daniel@0: CovMatrix = cov(Corx); Daniel@0: fclose(fidCov);