May 24, 2009

cell and struct in Matlab

Cell 规则
注意区分 () 和 {} indexing
() 生成另一个 cell
{} 生成 comma separated list (或者单个 cell 元素)

C(1) 是 1x1 cell
C{1} 是 第一个 cell 的元素

deal: deal 的参数为 comma separated list
[a b c] = deal(C{:})


Struct 规则
S = struct('f1','Fuck','f2',3); 生成 f1, f2 为元素名的 1x1 struct

增加一个
S(2).f1 = 4;
S(2).f2 = 5;

取某一个
S(i)

%How to use the comma seperated lists
S.f2

%from cell to struct
%NOTE the ???
S2 = cell2struct(A,{'f1','f2','f3'},2);

%from struct to cell
% the size of B is [#_of_fields, size(S)]
B = struct2cell(S);

0 comments: