Home > FMAToolbox > Database > Store.m

Store

PURPOSE ^

Store - Store variable in memory to avoid redundant computations.

SYNOPSIS ^

function Store(variable,name,varargin)

DESCRIPTION ^

Store - Store variable in memory to avoid redundant computations.

  This function is useful in the following scenario. When processing data
  in batches, a function is called repeatedly with different parameters.
  Because not all parameters change every time, some computations may be
  repeated across successive function calls. For time-consuming computations,
  (e.g. band-pass filtering of local field potentials) this represents a
  significant waste of time.

  Using Store and <a href="matlab:help Recall">Recall</a> solves this issue: you can 'store' the results of
  a computation within a function, and 'recall' it next time the function
  is called. Note that the variable is stored in memory, not on disk.

  USAGE

    Store(variable,name,key1,key2...)

    variable       result to store
    name           user-defined (arbitrary) name for the variable
    key1,key2...   values of the parameters used to compute the variable

  EXAMPLE

    function y = ThetaPhaseLocking(tetrode,cluster,channel)

      % First, try to recall the instantaneous phase for this
      % channel (it may have been computed in a previous call)

      phase = Recall('phase',channel);

      if isempty(phase),

        % Recalling failed, compute the phase now

        lfp = GetLFP(channel);
        theta = FilterLFP(lfp,'passband','theta');
        phase = Phase(theta);

        % Store the results for next time

        Store(phase,'phase',channel);

      end
      ...

  NOTE

    Use parsimoniously to avoid saturating memory.

  SEE

    See also StartBatch, GetBatch, BatchInfo, CancelBatch, CleanBatches, Recall.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function Store(variable,name,varargin)
0002 
0003 %Store - Store variable in memory to avoid redundant computations.
0004 %
0005 %  This function is useful in the following scenario. When processing data
0006 %  in batches, a function is called repeatedly with different parameters.
0007 %  Because not all parameters change every time, some computations may be
0008 %  repeated across successive function calls. For time-consuming computations,
0009 %  (e.g. band-pass filtering of local field potentials) this represents a
0010 %  significant waste of time.
0011 %
0012 %  Using Store and <a href="matlab:help Recall">Recall</a> solves this issue: you can 'store' the results of
0013 %  a computation within a function, and 'recall' it next time the function
0014 %  is called. Note that the variable is stored in memory, not on disk.
0015 %
0016 %  USAGE
0017 %
0018 %    Store(variable,name,key1,key2...)
0019 %
0020 %    variable       result to store
0021 %    name           user-defined (arbitrary) name for the variable
0022 %    key1,key2...   values of the parameters used to compute the variable
0023 %
0024 %  EXAMPLE
0025 %
0026 %    function y = ThetaPhaseLocking(tetrode,cluster,channel)
0027 %
0028 %      % First, try to recall the instantaneous phase for this
0029 %      % channel (it may have been computed in a previous call)
0030 %
0031 %      phase = Recall('phase',channel);
0032 %
0033 %      if isempty(phase),
0034 %
0035 %        % Recalling failed, compute the phase now
0036 %
0037 %        lfp = GetLFP(channel);
0038 %        theta = FilterLFP(lfp,'passband','theta');
0039 %        phase = Phase(theta);
0040 %
0041 %        % Store the results for next time
0042 %
0043 %        Store(phase,'phase',channel);
0044 %
0045 %      end
0046 %      ...
0047 %
0048 %  NOTE
0049 %
0050 %    Use parsimoniously to avoid saturating memory.
0051 %
0052 %  SEE
0053 %
0054 %    See also StartBatch, GetBatch, BatchInfo, CancelBatch, CleanBatches, Recall.
0055 %
0056 
0057 % Copyright (C) 2011 by Michaël Zugaro
0058 %
0059 % This program is free software; you can redistribute it and/or modify
0060 % it under the terms of the GNU General Public License as published by
0061 % the Free Software Foundation; either version 3 of the License, or
0062 % (at your option) any later version.
0063 
0064 % (using weird names makes it unlikely that this global could be used somewhere else)
0065 global storage_Store_Recall_981;
0066 
0067 stack = dbstack;
0068 if length(stack) < 2,
0069     functionName = 'CommandWindow';
0070 else
0071     functionName = stack(2).name;
0072 end
0073 
0074 if ~exist('storage_Store_Recall_981'),
0075     storage_Store_Recall_981 = struct;
0076 end
0077 
0078 % Remove previously stored variable with this name, if any
0079 if isfield(storage_Store_Recall_981,functionName),
0080     topFieldName = ['storage_Store_Recall_981.' functionName];
0081     topField = eval(topFieldName);
0082     if isfield(topField,name), eval([topFieldName ' = rmfield(' topFieldName ',''' name ''');']); end
0083 end
0084 
0085 % Store variable
0086 storage_Store_Recall_981 = setfield(storage_Store_Recall_981,functionName,name,'x',variable);
0087 
0088 % Store keys
0089 for i = 1:length(varargin),
0090     storage_Store_Recall_981 = setfield(storage_Store_Recall_981,functionName,name,['key' int2str(i)],varargin{i});
0091 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005