在MATLAB中保存用户定义的类

时间:2011-12-13 01:08:33

标签: arrays class matlab multidimensional-array

我在matlab中使用classdef作为,

classdef scores
    %   SCORES Summary of this class goes here
    %   Class for stroring the individual scores array of each of the
    %   comparison of the models and the test files including the number of
    %   coefficients used and number of gaussian 

    properties
        detailed_scores;
        no_of_gauss=0;
    end

    methods
    end
end

创建对象并以数组形式保存到数组中;

for i=1:99
    score = scores;
    score.detailed_scores = somescore(:,9);
    score.no_of_gauss = i;
    array = [array score];
end

现在,我想把它保存到matfile中作为;

save('somematfile.mat','array');

以后可以加载此array以检索数据吗?

1 个答案:

答案 0 :(得分:6)

是的,load somematfile就是这么做的。

load somemathfile
for k = 1:length(array)
  obj = array(k);
  obj.detailed_scores
end

但是,如果更改类定义,则必须小心,尤其是在删除/重命名或向类添加新属性时。在这种情况下,您可能需要实施saveobjloadobj方法。