首先,让我们将数字读取为字符串:
C = textread('myfile.txt', '%s', 'delimiter', '\n');
结果是一个字符串元胞数组,所以让我们应用一下str2num
在每个单元格上获取数值:
C = cellfun(@str2num, C, 'Uniform', false);
现在让我们丢弃每个单元格中的第一个元素:
C = cellfun(@(x)x(2:end), C, 'Uniform', false);
最后,我们将所有值连接成一个向量:
arr = [C{:}]
这是完整的代码:
C = textread('test.txt', '%s', 'delimiter', '\n'); %// Read data
C = cellfun(@str2num, C, 'Uniform', false); %// Convert to numbers
C = cellfun(@(x)x(2:end), C, 'Uniform', false); %// Remove first values
arr = [C{:}]
arr =
1.0000 1.4000 1.7000 1.2000 1.5000 1.1000 1.1000 1.2000