单元格上的数字子串

时间:2014-03-23 21:57:06

标签: matlab

我有一系列这样的数字:

My_array= [19 52 89 34 75 62]

我想做的是: 首先,逐个细胞取值。例如,第一个是:19

之后,我想在变量中保存数字(1)的第一个数字,在另一个变量中保存第二个数字(9)

我尝试使用以下代码执行此操作:

   for i=1:6
       element=my_array{i};
       elmt=num2str(element);
       var1=substring(elmt,1,1);
       var2=substring(elmt,2,1);
   end

但是我收到以下错误:

Error :  undefined function 'substring' for input arguments of type 'char'

所以,我的问题是:   在开始时,每个元素的类型都是单元格,不是 是否有从单元格转换为字符串的函数? 否则,为什么我的元素被认为是char而不是String? 如何修改我的代码用于上述目的? 谢谢!

1 个答案:

答案 0 :(得分:2)

<强>代码

My_array= [19 52 89 34 75 62]; %%// Input
arr1 = int2str(My_array)-'0'; %%// Separate the digits
arr1 = arr1(arr1~=-16); %%// Remove the numbers that represent the spaces between digits and numbers(-16)
arr1 = reshape(arr1,2,[])'; %%//' Reshape the digits into a Nx2 matrix
var1 = arr1(:,1); %%// Get the first digits into var1
var2 = arr1(:,2); %%// Get the second digits into var2