Ryderc
Ryderc

Reputation: 109

Dividing a cell into single column matrices

I know this looks trivial, but after hours browsing the web for an answer I am starting to give up!

I am reading an ASCII (string + double) file using Matlab in order to retrieve the data I need. I use the code below:

fid = fopen(filename,'rt');
output_data = textscan(fid,'%s','Delimiter','\t');

It gives me a cell 484 x 1. Up to now, it is as expected.

 3.0  12.605    37.815   6.173    700.000     0.567     0.4212  0.8422  0.3014    74.897   49.907    30.974     -0.004   11.483        17.374     15.066     33.257   0.1870          50.605      0.8540  92.2821
 3.5  10.804    37.815   6.173    700.000    -0.729     0.4593  0.8480  0.3051   129.674   68.391    77.109     -0.002   15.490        21.910     15.165     79.908   0.2830          68.236      0.8461  92.5326
 4.0   9.454    37.815   6.173    700.000    -1.674     0.4731  0.8160  0.2855   199.409   85.954   138.884     -0.005   19.052        26.176     15.297    142.077   0.3371          86.101      0.8174  92.9654
 4.5   9.198    41.389   6.756    766.150    -1.351     0.4734  0.7812  0.2661   284.050  104.149   215.800     -0.004   22.081        30.708     15.461    220.582   0.3676         105.286      0.7897  94.0197
 5.0   8.993    44.963   7.340    832.300    -1.475     0.4729  0.7728  0.2617   389.300  127.195   312.298     -0.011   25.463        35.872     15.667    318.630   0.3871         128.221      0.7790  95.3147
 5.5   8.992    49.457   8.073    915.500    -1.475     0.4729  0.7728  0.2617   518.157  153.904   431.486     -0.015   28.794        41.955     15.922    439.302   0.4010         154.751      0.7770  96.8529

Now it gets tricky: I want to create a matrix per "column". As you can notice, the space between two columns varies depending on the number of characters.

Meaning that I need a column:

3.0    
3.5  
4.0  
etc...

The next one need to be:

12.605  
10.804  
9.454  
etc...  

And so on until the last one

92.2821  
92.5326  
92.9654  
etc...  

I found this piece of code which can be of help, but I am unable to modify it to cope with more than 2 columns. Any help here would be much appreciated.

str_cellarr ={
'12:34'
'13:45'
'12:45'};
split1 = regexp(str_cellarr, ':', 'split');

split1_2cols = mat2cell(vertcat(split1{:}),size(str_cellarr,1),[1 1]);
[var1,var2] = deal(split1_2cols{:});
var1 = str2double(var1) 
var2 = str2double(var2)

Upvotes: 2

Views: 53

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

I assume you have a cell array of strings:

C = {'3.0  12.605    37.815   6.173    700.000     0.567     0.4212  0.8422  0.3014    74.897   49.907    30.974     -0.004   11.483        17.374     15.066     33.257   0.1870          50.605      0.8540  92.2821'
     '3.5  10.804    37.815   6.173    700.000    -0.729     0.4593  0.8480  0.3051   129.674   68.391    77.109     -0.002   15.490        21.910     15.165     79.908   0.2830          68.236      0.8461  92.5326'
     '4.0   9.454    37.815   6.173    700.000    -1.674     0.4731  0.8160  0.2855   199.409   85.954   138.884     -0.005   19.052        26.176     15.297    142.077   0.3371          86.101      0.8174  92.9654'
     '4.5   9.198    41.389   6.756    766.150    -1.351     0.4734  0.7812  0.2661   284.050  104.149   215.800     -0.004   22.081        30.708     15.461    220.582   0.3676         105.286      0.7897  94.0197'
     '5.0   8.993    44.963   7.340    832.300    -1.475     0.4729  0.7728  0.2617   389.300  127.195   312.298     -0.011   25.463        35.872     15.667    318.630   0.3871         128.221      0.7790  95.3147'
     '5.5   8.992    49.457   8.073    915.500    -1.475     0.4729  0.7728  0.2617   518.157  153.904   431.486     -0.015   28.794        41.955     15.922    439.302   0.4010         154.751      0.7770  96.8529'};

Then

M = regexp(c, '\s+', 'split'); %// split at spaces. Gives cell array of cell arrays of strings
M = vertcat(M{:}); %// convert into 2D cell array of strings
M = str2double(M); %// convert into numeric 2D array (matrix)

gives a matrix with all the numbers:

M = 
  Columns 1 through 12
    3.0000   12.6050   37.8150    6.1730  700.0000    0.5670    0.4212    0.8422    0.3014   74.8970   49.9070   30.9740
    3.5000   10.8040   37.8150    6.1730  700.0000   -0.7290    0.4593    0.8480    0.3051  129.6740   68.3910   77.1090
    4.0000    9.4540   37.8150    6.1730  700.0000   -1.6740    0.4731    0.8160    0.2855  199.4090   85.9540  138.8840
    4.5000    9.1980   41.3890    6.7560  766.1500   -1.3510    0.4734    0.7812    0.2661  284.0500  104.1490  215.8000
    5.0000    8.9930   44.9630    7.3400  832.3000   -1.4750    0.4729    0.7728    0.2617  389.3000  127.1950  312.2980
    5.5000    8.9920   49.4570    8.0730  915.5000   -1.4750    0.4729    0.7728    0.2617  518.1570  153.9040  431.4860
  [...]

So your desired column matrices are just M(:,1), M(:,2) etc

Upvotes: 2

Related Questions