将单元格划分为单列矩阵

时间:2015-09-24 10:43:41

标签: matlab matrix cell

我知道这看起来微不足道,但是经过几个小时浏览网页寻找答案我开始放弃了!

我正在使用Matlab读取ASCII(字符串+双精度)文件,以便检索我需要的数据。我使用下面的代码:

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

它给了我一个484 x 1的单元格。到目前为止,它是预期的。

 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

现在它变得棘手:我想为每个“列”创建一个矩阵。您可以注意到,两列之间的空格因字符数而异。

这意味着我需要一个专栏:

3.0    
3.5  
4.0  
etc...

下一个需要:

12.605  
10.804  
9.454  
etc...  

等到最后一个

92.2821  
92.5326  
92.9654  
etc...  

我发现这段代码可以提供帮助,但我无法对其进行修改以应对超过2列。这里的任何帮助将不胜感激。

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)

1 个答案:

答案 0 :(得分:2)

我假设你有一个字符串的单元格数组:

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'};

然后

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)

给出一个包含所有数字的矩阵:

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
  [...]

因此,您所需的列矩阵仅​​为M(:,1)M(:,2)