在matlab中保存和读取巨大的结构?

时间:2012-12-05 02:30:07

标签: matlab serialization struct save

在matlab中,我有一个程序this implementation的线性支持向量机来进行机器学习。

库在训练阶段输出代表模型的结构。该结构将用于测试阶段。

我想将此结构保存到文件中,因此我不必每次都要经历培训阶段。

我已经尝试了psychtoolbox' WriteStructsToText()ReadStructsFromText(),但是在将结构读入内存时由于缓冲区溢出而失败。

输出的结构是非常大量的数据(几十兆字节),所以这可能是个问题。

结构:

    -Parameters: Parameters
    -nr_class: number of classes; = 2 for regression
    -nr_feature: number of features in training data (without including the bias term)
    -bias: If >= 0, we assume one additional feature is added to the end
        of each data instance.
    -Label: label of each class; empty for regression
    -w: a nr_w-by-n matrix for the weights, where n is nr_feature
        or nr_feature+1 depending on the existence of the bias term.
        nr_w is 1 if nr_class=2 and -s is not 4 (i.e., not
        multi-class svm by Crammer and Singer). It is
        nr_class otherwise.

修改

任何人都知道如何解决此错误?

>> save('mode.txt','-struct','model');
>> model = load('mode.txt');
??? Error using ==> load
Number of columns on line 1 of ASCII file C:\Users\jason\Dropbox\Homework\cs280\FINAL PROJECTO\mode.txt
must be the same as previous lines.

1 个答案:

答案 0 :(得分:2)

最简单的方法是将结构保存为.mat文件:

outputStructure = yourTrainingFunction;

save('someFileName.mat','-struct','outputStructure');

加载

outputStructure = load('someFileName.mat')

请注意,如果您的文件非常大,您可能必须在Matlab的常规设置中设置正确的兼容性(7.3之前的保存文件版本无法处理文件> 2GB,如果我没记错的话)。