我有一个文本文件,其内容如下所示。我需要按列顺序读取此文件(ies,2列)。我尝试了很多方法..但不能做,因为它包含“(”,“,”,“)”等...请指导..
(-2.714141687294326, 0.17700122506478025)
(-2.8889905690592976, 0.1449494260855578)
(-2.74534285564141, 0.3182989792519164)
(-2.728716536554531, -0.3267545129349194)
(-2.280859632844493, -0.7413304490629143)
(-2.8205377507406095, 0.08946138452856946)
(-2.6261449731466335, -0.16338495969832847)
(-2.8863827317805537, 0.5783117541867042)
(-2.6727557978209546, 0.11377424587411682)
(-2.5069470906518565, -0.6450688986485736)
(-2.6127552309087236, -0.01472993916137419)
(-2.7861092661880185, 0.23511200020171835)
(-3.2238037438656533, 0.5113945870063824)
(-2.6447503899420304, -1.1787646364375748)
答案 0 :(得分:1)
试试这个:
x = importdata('filename.txt');
x = regexp(x,'-?\d+\.?\d*','match'); %// detect numbers as [-]a[.][bcd]
x = cellfun(@str2num, vertcat(x{:}));
如果文件可以包含十进制形式的数字(“1.234”)和科学记数法(“1.234e-56”):
x = importdata('filename.txt');
x = regexp(x,'-?\d+\.?\d*(e-?\d+)?','match');
x = cellfun(@str2num, vertcat(x{:}));
答案 1 :(得分:1)
您可以使用fscanf
并指定所需的格式:
fid = fopen('filename.txt');
x = fscanf(fid,'(%f, %f)\n',[2,inf]).';
fclose(fid);
格式规范'(%f, %f)\n'
读取括号内的浮点值,每行以,
分隔。使用[2,inf]
指定将其放入2 x n
数组,其中n
与所需数量一样大。要使用与之前相同的格式,您必须再次转置.'
。