view m-toolbox/classes/@ao/fromDataInMAT.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line source

% FROMDATAINMAT Convert a saved data-array into an AO with a tsdata-object
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    fromDataInMAT
%
% DESCRIPTION: Convert a saved data-array into an AO with a tsdata-object
%
% CALL:        obj = fromLISO(obj, data-array, plist)
%
% PARAMETER:   data-array: data-array
%              plist:      plist-object (must contain the filename)
%
% VERSION:     $Id: fromDataInMAT.m,v 1.8 2011/08/12 11:38:03 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function objs = fromDataInMAT(obj, loadData, pli)
  
  VERSION = '$Id: fromDataInMAT.m,v 1.8 2011/08/12 11:38:03 hewitson Exp $';
  
  ii = obj.getInfo(class(obj), 'From MAT Data File');
  
  % Set the method version string in the minfo object
  ii.setMversion([VERSION '-->' ii.mversion]);
  
  %%%%%%%%%%   Get default parameter list   %%%%%%%%%%
  dpl = ii.plists();
  pl  = applyDefaults(dpl, pli);
  
  % Get filename
  filename = find(pli, 'filename');
  [pathstr, f_name, ext] = fileparts(filename);
  
  pl = pset(pl, 'filename', [f_name ext]);
  pl = pset(pl, 'filepath', pathstr);
  
  data_type = find (pl, 'type');
  columns   = find (pl, 'columns');
  fs        = find (pl, 'fs');
  
  objs = [];
  
  %%%%
  if strcmpi(data_type, 'cdata')
    fs = 1;
  end
  
  % Then we try for a numerical data set in
  % the first numerical field we come to
  fnames = fieldnames(loadData);
  for jj=1:length(fnames)
    if isnumeric(loadData.(fnames{jj}))
      % get the data from here
      data = loadData.(fnames{jj});
      
      if isempty(columns)
        columns = 1:size(data,2);
      end
      
      if max(columns) > size(data,2)
        error('### The stored variable [%s] doesn''t contain %d columns. It only contains %d columns.', fnames{jj}, max(columns), size(data,2));
      end
      
      if isempty(fs)
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%                       Create from x and y                       %%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        for kk=1:2:numel(columns)
          
          % Create an empty object.
          obj = obj.initObjectWithSize(1,1);
          
          x = data(:, columns(kk));
          y = data(:, columns(kk+1));
          
          switch lower(data_type)
            case 'tsdata'
              dataObj = tsdata(x, y);
            case 'fsdata'
              dataObj = fsdata(x, y);
            case 'xydata'
              dataObj = xydata(x, y);
            case 'cdata'
              error('### Should not happen');
            otherwise
              error('### unknown data type ''%s''', data_type);
          end
          
          obj.data = dataObj;
          
          plh = pl.pset('columns', [columns(kk) columns(kk+1)]);
          if isempty(pl.find('Name'))
            plh.pset('Name', sprintf('%s_%d_%d', find(pl, 'filename'), columns(kk), columns(kk+1)));
          end
          
          obj.addHistory(ii, plh, [], []);
          
          objs = [objs obj];
          
        end
        
      else
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%                       Create from y and fs                      %%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        for kk=1:numel(columns)
          
          % Create an empty object.
          obj = obj.initObjectWithSize(1,1);
          
          y = data(:, columns(kk));
          
          switch lower(data_type)
            case 'tsdata'
              dataObj = tsdata(y, fs);
            case 'fsdata'
              dataObj = fsdata(y, fs);
            case 'xydata'
              dataObj = xydata(y);
            case 'cdata'
              % Special case for cdata objects.
              % If the user doesn't specify any columns then return only
              % one AO with all the data. But if the user defines any
              % columns then return for each column an AO
              pl.removeKeys({'fs', 'xunits'});
              if isempty(pl.find('columns'))
                obj = obj.initObjectWithSize(1,1);
                obj.data = cdata(data);
                obj.name = pl.find('filename');
                obj.addHistory(ii, pl, [], []);
                objs = [objs obj];
                break;
              else
                dataObj = cdata(data(:,columns));
              end
            otherwise
              error('### unknown data type ''%s''', data_type);
          end
          
          obj.data = dataObj;
          
          plh = pl.pset('columns', columns(kk));
          if isempty(pl.find('Name'))
            plh.pset('Name', sprintf('%s_%d', find(pl, 'filename'), columns(kk)));
          end
          
          % add history
          obj.addHistory(ii, plh, [], []);
          
          objs = [objs obj];
        end
        
      end
      
      % set any object properties
      objs.setObjectProperties(pl);
      
    end % End if the mat file contains numeric data
  end % End loop over filenames
  
  
end