从MATLAB中的文本文件中读取空白和行空间分隔复数

时间:2016-01-26 20:25:37

标签: matlab textscan

我在text file

的四列中有四个复数的流

我必须以相同的格式将它们读入MATLAB矩阵。

我尝试了以下操作,但它不起作用

fid =  fopen('~/<path-to-file>/<fileName>.txt','r');
out = textscan(fid, '(%f,%f) \b\t(%f,%f) \b\t(%f,%f) \b\t(%f,%f) \n','CollectOutput',1);
tapWeights = [out{1} + 1i*out{2} out{3} + 1i*out{4} out{5} + 1i*out{6} out{7} + 1i*out{8}];
fclose(fid);

请注意,textscan的帮助选项会将\b\t列为空白分隔符。

以下是我得到的输出,

>> out

out = 

    [1x8 double]

>> out{1}

ans =

     1     0   NaN   NaN   NaN   NaN   NaN   NaN

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

除非我遗漏了某些内容,否则简单的regexpstr2doublecellfunreshape就可以解决问题(假设矩阵的结构已知,以便重新塑造它

s='(1.00000000,0.00000000) (1.00000000,0.00000000) (-0.00000004,1.00000000) (-0.00000004,-1.00000000) (0.66912299,0.74315202) (0.66912299,0.74315202) (-0.74315202,0.66912293) (0.74315202,-0.66912305) (-0.10454900,0.99452001) (-0.10454900,0.99452001) (-0.99452001,-0.10454904) (0.99452001,0.10454895) (-0.80903500,0.58776098) (-0.80903500,0.58776098) (-0.58776093,-0.80903500) (0.58776104,0.80903500) (-0.97813898,-0.20795099) (-0.97813898,-0.20795099) (0.20795104,-0.97813898) (-0.20795095,0.97813898) (-0.49995700,-0.86605000) (-0.49995700,-0.86605000) (0.86605000,-0.49995697) (-0.86605000,0.49995703) (0.30907401,-0.95103800) (0.30907401,-0.95103800) (0.95103800,0.30907404) (-0.95103800,-0.30907398) (0.91357303,-0.40667400) (0.91357303,-0.40667400) (0.40667397,0.91357303) (-0.40667403,-0.91357303) (0.30893400,0.95108300)';
a=reshape(cellfun(@(x) str2double(x{1})+str2double(x{2})*1j,regexp(s,'[(]([^,]*),([^)]*)[)]','tokens')),3,[])'