0
votes

What I am trying to do is, to load multiple 'network' files and assign them to variables, so that I can access their internal parameters (weights, bias etc.). When I try to load each like this:

a = load('networkFileName')

Then, variable 'a' becomes a struct with a network file inside, which cannot have its parameters accessed, unless you call:

a.net123

Is there another way to load the network variable directly in another variable?

2

2 Answers

1
votes

See the documention:

help load

(Re comment) Yes there is information, directly copied from the help:

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

To expand on this: If you don't specify an output the workspace:

>> 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              

This can be dangerous as you don't know what is definataly in the .mat file so there is the potential for it to overwrite workspace variables and cause your code to crash... or not to have the variable your interested in and cause your code to crash...

In response to your own answer - you should look up dynamic fieldnames as a more modern version of getfield.

1
votes

Ok, I figured it out. The fields of a struct can be accessed with 'fieldnames' and then you can use 'getfield' to access the field's respective variable. In my case:

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

EDIT: Using dynamic fieldnames according to matlabgui's suggestion.

>> 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