2016-02-23 18 views
0

我所試圖做的是,加載多個「網絡」文件,並將其分配給變量,這樣我可以訪問其內部參數(重量,偏差等) 。當我嘗試加載每個這樣的:MATLAB - 負載網絡的文件和是直接分配給變量

a = load('networkFileName') 

然後,變量a變成裏面的網絡文件,這是不能有它的參數訪問的結構,除非你撥打:

a.net123 

是還有另一種方法直接在另一個變量中加載網絡變量?

回答

1

好吧,我想通了。一個結構的字段可以用「字段名」訪問,然後你可以使用「getfield命令」訪問現場的各個變量。在我的情況:

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

編輯:使用dynamic fieldnames根據matlabguisuggestion

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

見機制的文檔:

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文件中的definataly是什麼,所以那裏是因爲它覆蓋的工作空間變量和導致代碼崩潰......或不具有可變的興趣,使你的代碼崩潰......

在回答自己的答案的潛力 - 你應該看看達dynamic fieldnames作爲一個更現代版的getfield

+0

在'加載'文檔中沒有關於此的信息 – yannovios

+0

(重新編輯)好的,謝謝你的額外信息:) – yannovios