我正在尝试将一系列随机生成的数字分配给字符串数组,但得到错误:
Conversion to char from cell is not possible.
Error in work (line 11)
first_gen(i) = num2cell(str_r);
我正在使用for循环迭代10次并希望完全填充我的数组,这是故障代码:
first_gen = ['';'';'';'';'';'';'';'';'';''];
% our first generation of ants
for i = 1:10
str_r = sprintf('%d', randi([0,9],1,30));
% modify selected characters.
str_r(1:3:end) = sprintf('%d', randi([1,4],1,10));
first_gen(i) = num2cell(str_r);
end
有谁知道为什么会这样?我尝试过使用花括号,如下所示:
first_gen(i) = {num2cell(str_r)};
但这只会产生同样的错误。
感谢。
答案 0 :(得分:0)
目前,您的first_gen
是一个矢量数组,而不是一个单元格数组。你不能将一个单元格存储在一个向量中。您需要单元格来存储单元格如下:
first_gen = cell(1,10);
% our first generation of ants
for i = 1:10
str_r = sprintf('%d', randi([0,9],1,30));
% modify selected characters.
str_r(1:3:end) = sprintf('%d', randi([1,4],1,10));
first_gen{i} = num2cell(str_r);
end
first_gen
是细胞的细胞:
first_gen =
Columns 1 through 8
{1x30 cell} {1x30 cell} {1x30 cell} {1x30 cell} {1x30 cell} {1x30 cell} {1x30 cell} {1x30 cell}
Columns 9 through 10
{1x30 cell} {1x30 cell}