Search code examples
matlabsplitcell-array

How to split cell array values into two columns in MATLAB?


I have data in a cell array as shown in the variable viewer here:

{[2.13949546690144;56.9515770543056],
 [1.98550875192835;50.4110852121618],
 ...}

I want to split it into two columns with two decimal-point numbers as:

2.13   56.95 
1.98   50.41

by removing opening and closing braces and semicolons such as [;] (to do as like "Text to columns" in Excel).


Solution

  • If your N-element cell array C has 2-by-1 numeric data in each cell, you can easily convert that into an N-by-2 numeric matrix M like so (using the round function to round each element to 2 significant digits):

    M = round([C{:}].', 2);
    

    The syntax C{:} creates a comma-separated list of the contents of C, equivalent to C{1}, C{2}, ... C{N}. These are all horizontally concatenated using [ ... ], then the result is transposed using .'.