1.创建一个元胞数组testcell
方式1:
testcell = cell(2,3);
testcell{1,1} = {'monday','mon'};
testcell{1,2} = {'tuesday ','tue'};
testcell{1,3} = {'wednesday','wed'};
testcell{2,1} = {'thursday','thu'};
testcell{2,2} = {'friday','fri'};
testcell{2,3} = {'saturday','sat'};
方式2:
testcell = { {'monday','mon'},{'tuesday ','tue'},{'wednesday','wed'};{'thursday','thu'},{'friday','fri'},{'saturday','sat'}};
2.读取元素
>> testcell(1,1)
ans =
1×1 cell 数组
{1×2 cell}
>> testcell{1,1}(1,1)
ans =
1×1 cell 数组
{'monday'}
>> testcell{1,1}(1,2)
ans =
1×1 cell 数组
{'mon'}
3.判断元胞数组某个元素是否与某个字符串相等:先用cellstr转为字符串,再用strcmp比较字符串
>> strcmp(cellstr(testcell{1,1}(1,1)),'sunday')
ans =
logical
0
>> strcmp(cellstr(testcell{1,1}(1,1)),'monday')
ans =
logical
1
错误比较方法:
>> cellstr(testcell{1,1}(1,1))=='sunday'
未定义与 'cell' 类型的输入参数相对应的运算符 '=='。