1. 赋值 对于Matlab中的单元数组纠结了好几天,正确的赋值方法为: cellArray1 = {cellArray2{I}}; 其中I为索引素组,用于重排cellArray2中cell的顺序或者按要求提取cellArray2数组中的单元。注意cellArray2{I}必须用大括号{}括起来,否则得到的仅仅是cellArray2中的第一个单元。 以下是一些可行的但不可用的赋值方法 [cell1 cell2 cell3] = cellArray2{1:3}; cellArray1{1} = cell1; cellArray{2} = cell2; cellArray{3} = cell3; 构建一个大小为N*M,且值为'data'的单元数组: c1 = cell(N, M); c1(:) = {'data'}; 1.2 连续赋值 对于cell数组,连续赋值可以用一下格式 例1:赋以连续数字 >> test=cell(1,3); >> test(1,1:3)=num2cell(1:3:9) test = [1] [4] [7] cell数组中每一元素都是一个cell结构的,所以可以连续索引cell单元,赋以连续的cell结构的数据。注意不能用内容索引,即大括号{ },如test{1:3},会报错 >> test{1:3}=num2cell(1:3:9) The right hand side of this assignment has too few values to satisfy the left hand side. >> test{1:3}=1:3:9 The right hand side of this assignment has too few values to satisfy the left hand side. 2. 操作 cellfun函数可以遍历地执行单元数组的每个元素,matlab内部应该会根据CPU核数进行加速,因为文档说执行的先后顺序不确定。 例子:将source文件夹下的1,4,7,...100.jpg拷贝到dest文件夹下 f = @(n) ['source/' num2str(n) '.jpg']; src = cellfun(f, num2cell(1:3:100), 'UniformOutput', false); des = cell(size(src)); des(:) = {'dest/'}; cellfun(@copyfile, src, des, 'UniformOutput', false); % 完毕 3. 相关函数 num2cell : 将数组转化为单元数组 mat2cell : 将矩阵分割为单元数组,其中单元数组的元素也是矩阵 cell2mat :
celldisp:显示所有的内容 cellplot:利用图形方式显示内容 cell2struct:将数组转变成为结构 struct2cell:将结构转变为cell数组 iscell:判断输入是否为cell数组 |
|