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