Search code examples
matlabnumber-formattingcell-array

Formatting cell to double


I have text file in which the data is like (1,2),(3,4),1 (4,5),(6,7),1 I am reading the file in the matlab as:

A= textread('abc.txt', '%s');

the output is

A: 2x1 cell
(1,2),(3,4),1 
(4,5),(6,7),1

Anyone please help to convert this cell to double with the output

New_A= 5x2 double
1 2 3 4 1 
4 5 6 7 1

Solution

  • For your particular example, once you read the file, you can use textscan.

        A = {'(1,2),(3,4),1' 
             '(4,5),(6,7),1'};
    
        New_A = cell2mat(cellfun(@(line) cell2mat(textscan(line,'(%f,%f),(%f,%f),%f')), A, 'UniformOutput', 0));
    
    
    New_A =
    
         1     2     3     4     1
         4     5     6     7     1
    

    You can also do similar, line by line, as they are read from the file.