I have data under headings with underscore.i e. tce500_a,tce500_b... How can I read data under tce500_c. The '_' is not recotnized and using t500 reads all of them.Thanks
4 次查看(过去 30 天)
显示 更早的评论
t500_a
0 50
0.5 40
I want to read the data under each heading distinguished by _a,_b,_c, etc.
采纳的回答
Walter Roberson
2017-11-21
S = fileread('YourFileName.txt');
[headers, block_start] = regexp(S, '^[^\n]*_[^\n]*', 'lineanchors', 'match');
block_bounds = [block_start, length(S)+1];
headers_to_extract = {'t500_c', 'ta719_d'};
num_to_extract = length(headers_to_extract);
extracted_data = cell(num_to_extract, 1);
[tf, blkidx] = ismember(header_to_extract, headers);
for K = 1 : num_to_extract
if ~tf(K)
fprintf('Warning: header "%s" not found in file\n', headers_to_extract{K});
else
thisidx = blkidx(K);
thisblock = S( block_bounds(thisidx) : block_bounds(thisidx+1) - 1 );
extracted_data(K) = textscan(thisblock, '%f %f', 'HeaderLines', 1, 'CombineOutput', 1); %note rhs is a cell
end
end
The result will be a cell array, extracted_data, with as many entries as was in the list headers_to_extract, each entry of which is the relevant data from the file. In the case where the header was not found in the file, the entry will be empty. The cells will be in the same order as headers_to_extract
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!