ranell
ranell

Reputation: 703

Substring for number on a cell

I have an array of numbers like this:

My_array= [19 52 89 34 75 62]

What I want to do is: First, take the value cell by cell. For example, the first one would be : 19

After that, I want to save the first digit of the number (1) in a variable and the second digit (9) in another variable

I'm trying to do this with the following code:

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

But I get the following error:

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

So, my questions are: At the beggining the type of each element is cell, isn't ? Is there any function to convert from cell to string ? Otherwise, why is my element considered a char and not a String? How can I modify my code for the purpose described above ? Thanks!

Upvotes: 0

Views: 454

Answers (1)

Divakar
Divakar

Reputation: 221574

Code

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

Upvotes: 2

Related Questions