【问题标题】:Structures in matlab: save load and workmatlab中的结构:节省负载和工作
【发布时间】:2013-03-07 13:09:09
【问题描述】:

我正在尝试使用 matlab 中的结构。我有一个看起来像这样的代码:

for i=1:10
    a(i).p=some value;
    a(i).q=some other value
end

我将它保存到一个 mat 文件,但它似乎并不成功。谁能告诉我,如何保存并将此结构加载到文件/从文件中并读取特定类型的数据?例如,如何在加载结构后读取字段a(i).q? 谢谢

【问题讨论】:

    标签: matlab structure


    【解决方案1】:

    要保存和加载,请使用saveload

    for ii=1:10
        a(ii).p = rand(1);
        a(ii).q = rand(1);
    end
    save( 'myMatFile.mat', 'a' ); % note that the variable name is passed as a STRING
    
    clear a; % remove a from workspace. it is gone...
    exist( 'a', 'var' ), % make sure a is gone
    
    load( 'myMatFile.mat' ); % load 
    exist( 'a', 'var' ), % a now exists! Ta-da!!
    
    a(5).q, % access the fifth element of a
    

    PS
    It is best not to use i and j as variables in Matlab

    【讨论】:

    • 请注意 MATLAB 无法处理 save('file.mat','a.p') 。你需要做foo=a.p; save('file.mat','foo')
    • @CarlWitthoft 您可以通过save('file.mat','a','-struct') 获取保存文件中的变量p
    • 嗯,是的,save('file.mat',-struct,'a','p') 会起作用,但是为什么用户必须做解析器应该处理的事情呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2022-07-06
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多