Home > atmlab > files > loadstructfiles.m

loadstructfiles

PURPOSE ^

LOADSTRUCTFILES Loads and merge mat-files with data in structures

SYNOPSIS ^

function O = loadstructfiles(filenames,varargin)

DESCRIPTION ^

 LOADSTRUCTFILES   Loads and merge mat-files with data in structures

    The function is useful if you have data stored in different files,
    where each file contains a structure. The structure can have a
    different name in each file, but the field names shall be the
    same throughout. By using the optional argument, unnecessary fields 
    can be ignored.

    An example, where fileds 'a' and 'b' are wanted:
       filenames = whichfiles('*.mat');
       O = loadstructfiles(filenames,{'a','b'});

 FORMAT   O = loadstructfiles(filenames[,fnames])
        
 OUT   O           Structure array with all data.
 IN    filenames   Cell array of strings with names of files to read.
 OPT   fnames      Cell array of strings with names of fields to keep.
                   If not specified all fields will be kept.
       verbose     Set to 1 to get a simple file counter. Default is 0.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

loadstructfiles.m

SOURCE CODE ^

0001 % LOADSTRUCTFILES   Loads and merge mat-files with data in structures
0002 %
0003 %    The function is useful if you have data stored in different files,
0004 %    where each file contains a structure. The structure can have a
0005 %    different name in each file, but the field names shall be the
0006 %    same throughout. By using the optional argument, unnecessary fields
0007 %    can be ignored.
0008 %
0009 %    An example, where fileds 'a' and 'b' are wanted:
0010 %       filenames = whichfiles('*.mat');
0011 %       O = loadstructfiles(filenames,{'a','b'});
0012 %
0013 % FORMAT   O = loadstructfiles(filenames[,fnames])
0014 %
0015 % OUT   O           Structure array with all data.
0016 % IN    filenames   Cell array of strings with names of files to read.
0017 % OPT   fnames      Cell array of strings with names of fields to keep.
0018 %                   If not specified all fields will be kept.
0019 %       verbose     Set to 1 to get a simple file counter. Default is 0.
0020 
0021 % 2006-02-01   Created by Patrick Eriksson.
0022 
0023 
0024 function O = loadstructfiles(filenames,varargin)
0025 %
0026 [fnames,verbose] = optargs( varargin, { [], 1 } );
0027 
0028 
0029 n = 0;
0030 
0031 for i = 1 : length(filenames)
0032 
0033   if verbose
0034     fprintf( '%d/%d\n', i, length(filenames) );
0035   end
0036   
0037   %= Load data into a structure as we do not know name of variable
0038   %
0039   % We can then get the varaible name by *fieldnames* and use dynamic field
0040   % naming (all this to avoid using *eval*).
0041   %
0042   S = load( filenames{i} );
0043 
0044   %= Only one variable shall be found
0045   %
0046   vname = fieldnames(S);
0047   %
0048   if length(vname) > 1
0049     error(sprintf('More than 1 variable in %s',filenames{i}));
0050   end
0051 
0052   if isempty(fnames)
0053     for j = 1:length(S.(vname{1}))
0054       n = n + 1;
0055       O(n) = S.(vname{1})(j);
0056     end
0057   else
0058     for j = 1:length(S.(vname{1}))
0059       n = n + 1;
0060       for k = 1:length(fnames)
0061         O(n).(fnames{k}) = S.(vname{1})(j).(fnames{k});
0062       end
0063     end
0064   end
0065 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005