首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MATLAB中方便缓存以进行大数据分析。

在MATLAB中方便缓存以进行大数据分析。
EN

Stack Overflow用户
提问于 2014-07-28 15:56:26
回答 1查看 340关注 0票数 1

我将在MATLAB中同时运行20个函数/过程来分析一个大数据集。每个函数都访问这个大数据集的一部分。缓存是我的主要组件,我需要向您展示MATLAB可以使用缓存能力。我想:

  1. 每个函数默认保留一个缓存空间,以保存/修改/删除/查找缓存空间中的某些内容/数据。
  2. 每个函数都可以从其他函数保留的其他缓存空间中请求一些所需的内容/数据。

你能告诉我怎么可能吗?我需要一些提示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-28 18:33:39

检查persistent变量在MatLab (和mlock函数)中的文档。它部分涵盖了你所需要的东西。但是,在访问持久变量以及在更新函数源文件时清除持久性变量这一事实方面,您将面临相当大的问题。

我建议为您的缓存使用文件(当然,如果我正确理解您的意思)。例如,您可以从这种方法开始(我假设每个函数都位于单独的*.m文件中)

代码语言:javascript
复制
function CacheFileName = GenerateCacheFileName(Caller)
    CacheFileName = sprintf('%s.cache.mat',Caller);
    % you may use any alogrithm that makes sense for you
    % but keep the MAT extension for the simple syntax of LOAD function
end


function Data = LoadCachedData(Caller)
    % Generate cache file name for current function
    CacheFileName = GenerateCacheFileName(Caller);
    if exist(CacheFileName,'file')==2
        % load cache file if it exists
        RawData = load(CacheFileName);
        Data = RawData.CacheStructure;
    else
        % or initialize the cahce with empty structure
        Data = struct;
    end
end


function DoSomethingUsingCache(arguments)

    % Generate cache file name for current function
    CacheFileName = GenerateCacheFileName(mfilename);

    % Load cached data
    if exist(CacheFileName,'file'==2)
        % load cache file if it exists
        CacheStructure = load(CacheFileName);
    else
        % or initialize the cahce with current datestamp
        CacheStructure.Created = now;
    end

    % do what you need here

    % Save data to cache for later use
    save(CacheFileName,'CacheStructure');
end

如果您需要从其他函数加载一些数据,只需这样做:

代码语言:javascript
复制
function DoSomethingUsingCacheOfOtherFunction(arguments)

    % Load chached data of other function
    CacheStructure2 = LoadCachedData('DoSomethingUsingCache');

    % do what you need here

    if isfield(CacheStructure,'Param4')
        CacheStructure.Param4 = CacheStructure.Param4 + 10;
    else
        CacheStructure.Param4 = 0;
    end

    % you may also update the cache for other function if you need
    save(CacheFileName2,'CacheStructure2');
end

显然,应该正确设计这种方法,否则MatLab将花费大部分时间加载/保存您的数据。

原则上,这种方法可以作为具有set/get方法的MatLab类来表示,以获得更简单的代码。如果在根命名空间中创建对象,则可以将其用作“缓存的数据管理器”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24999184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档