我知道已经有很多关于这个话题的问题了。但所有解决方案都适用于某些情况。我试图找到一种高度通用的方法来从包含char.
示例数据:lines= <779x88 char>(后处理textscan数据)
xtotal=   0.3414E-03   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.0000E+00
xtotal=   0.7239E-03   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.1875E-08
...
xtotal=   0.1788E-01   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.9965E-06
xtotal=   0.2586E-01   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.1992E-05
以下循环正在做我想要的:
   n =  4;  %number of output values
off1 =  3;  %offset 1
off2 = 13;  %offset 2
L = size(lines,1);   %Length of cell array
index = strfind(lines(1,:),'=');   %find all indices in char for "="
value = zeros(L,n);                %pre-allocation
% read numerical values and write it into array
% for sure vectorization is possible, but that shouldn't be the topic now
for ii=1:L
    for jj=1:n  
    value(ii,jj) = str2double( lines(ii, index(jj)+off1:index(jj)+off2 ) );
    end
end
结果:
value = 
  0.0003       0         0         0
  0.0007       0         0    0.0000
...
183.1000       0         0   95.4900
183.1000       0         0   95.4900
虽然它适用于这种情况,但我仍然必须定义:
   n =  4;  %number of output values
off1 =  3;  %offset 1
off2 = 13;  %offset 2
我不想确定我正在处理的所有不同的输入文件。我唯一假设要预先确定的应该是分隔符“ =”,因为它对于所有输入文件都应该是相同的。那么没有任何可靠的方法来检测 char 数组中的数值数据吗?