我想从下面的txt文件中提取Andy的结果。我的期望是提取1.033,-0.017,1.016并将它们放入单元格数组中。有没有办法提取这些?我有大约100个文件,格式如下。
Total 6 outlier 0
| Thomas | -0.255 | -0.006 | -0.261 |
| Todd | 1.012 | 0.112 | 1.124 |
| Harry | -0.033 | 0.005 | -0.028 |
| Andy | 1.033 | -0.017 | 1.016 |
| Zheng | 0.152 | 0.226 | 0.378 |
| Betsi | -19.409 | 1.010 | -18.399 |
| Andrew | -0.066 | 0.048 | -0.018 |
| Tom | -95.582 | 0.590 | -94.991 |
答案 0 :(得分:0)
我假设您的文本文件中没有新行。
让我们尝试一下:
%Open you file and load the data
fid = fopen('the_path_of_your_file');
line = fgetl(fid);
%We split the data using the '||' delimiter. Each personne's results
%is stored in a cell.
txtSplitCell = strsplit(line,'||');
%Here we check in each cell if 'Andy' appears
indAndy = cellfun(@(c)strfind(c,'Andy')),...
txtSplitCell,'UniformOutput',false);
%We split the content of Andy's results cell with the second sperator '|'
%to get the values
resAndyRaw = strsplit(txtSplitCell{find(indAndy,1)},'|');
% We only select the subsection of the cell array that actually contains
%Andy's results (in other words we remove 'Andy' in the results cell
%and we convert that into matrix;
resAndyTab = cell2mat(resAndyRaw(strcmp(resAndyRaw,'Andy'):end));
%Note: strcmp(resAndyRaw,'Andy') enable locating the postion of
%'Andy' in the resulting cell array of the strsplit function.
% Consequently thus this code should work for thomas as well.
fclose(fid)
print(resAndyTab)
我无法运行该代码(我的计算机上没有matlab)。所以请测试它并在需要时进行纠正。
再见。