MATLAB - 加载网络文件并将它们直接分配给变量

MATLAB - load network files and assign them directly to variables

我想做的是加载多个 'network' 文件并将它们分配给变量,这样我就可以访问它们的内部参数(权重、偏差等)。当我尝试像这样加载每个时:

a = load('networkFileName')

然后,变量 'a' 变成一个内部有网络文件的结构,不能访问它的参数,除非你调用:

a.net123

有没有其他方法可以直接在另一个变量中加载网络变量?

查看文档:

help load

(重新评论) 有资料,直接从帮助中复制过来的:

    load(...) loads without combining MAT-file variables into a structure
array.

对此进行扩展:如果您不指定输出工作区:

>> whos net123                 % it doesn't exist
>> load ( 'net123.mat' )       % load a file which (may) have a var net123
>> whos net123                 % it now exists.
  Name        Size            Bytes  Class     Attributes

  net123      1x1                 8  double              

这可能很危险,因为您不知道 .mat 文件中肯定有什么,因此它有可能覆盖工作区变量并导致您的代码崩溃……或者不让您的变量感兴趣并导致您的代码崩溃...

针对您自己的回答 - 您应该查找 dynamic fieldnames 作为 getfield 的更现代版本。

好的,我明白了。可以使用 'fieldnames' 访问结构的字段,然后可以使用 'getfield' 访问字段的相应变量。就我而言:

>> a = load('networkFileName');
>> name = fieldnames(a);        % a cell with the Struct's field names
>> newVariable = getfield(a,name{1})

编辑: 使用 dynamic fieldnames according to matlabgui's .

>> a = load('networkFileName');
>> z = fieldnames(a);           % gets you a cell with a's fieldname
>> z = z{1};                    % in my case, the network is in the first cell field
>> newVariable = a.(z);         % this is the desired variable