2014-02-25 16 views
0

我有不同的測試案例的多個數據變量使得matfile將是例如matfile內保存變量各個結構:如何從一個Matlab Matfile是Matfile

zzz.mat 

而且在數據中是列爲:

C1_jjj 
C1_lll 
C1_ggg 

C2_jjj 
C2_lll 
C2_ggg 

jjjlllggg所代表的變量名,C1C2是不同的病例數。

我想重寫並保存matfile,以便在matfile中將每個測試用例變量(C1和C2)分解爲它們自己的結構。因此,matfile,zzz.mat現在將讀:

C1(structure) 
C2(structure) 

其中C1和C2含有每個變量從執行的每個測試(JJJ,LLL,GGG)。

關於如何重組這個想法?

+1

連續變量名('C1''C2')很少是個好主意。我會使用單元格或數組。 – Daniel

回答

0

你應該更進一步,並使用一個結構數組。你的matfile將包含一個變量,而C(1).jjj就是它指向一個變量的例子。
基本的想法是加載matfile作爲一個結構(以獲得一個簡單的變量列表),然後使用動態字段引用將結構的形式轉換爲更易讀的東西。

C = struct; 

% Load MATFILE into a single structure containing all the variables. 
S = load('zzz.mat'); 

% Loop over fields of S, which is the same as 
% variable names in the MATFILE 
fn = fieldnames(S); 
for ii = 1:length(fn) 
    f = fn{ii}; % original variable name, C1_jjj for exammple 

    % Extract case number and variable name from the string. 
    % Edit this if your variable names are different from 
    % the examples given 
    tokens = regexp(f, 'C(\d+)_(\w+)', 'tokens'); 
    case_number = str2num(tokens{1}{1}); 
    var_name = tokens{1}{2}; 

    % Insert the value of the field into the new structure 
    % using dynamic fieldnames 
    C(case_number).(var_name) = S.(f); 
end 
相關問題