How to exclude the characters row in a text file?
3 次查看(过去 30 天)
显示 更早的评论
In the attached file i want to read only the numeric data of the file so that i can plot X Y. But i am facing problem that how to offset the row having strings. Someone help !
3 个评论
采纳的回答
Walter Roberson
2019-1-1
The below code does not assume particular headers and does not assume that each group of numbers has the same number of columns, and does not assume empty space between groups, and does not assume any particular number of rows per group. It does, however, assume that each group has the same number of numeric columns within itself.
Output is a cell group_data with one entry per grouping, with the entry being a numeric array with multiple columns. Also output is a cell group_headers with one entry per grouping, with each entry being a cell array of column headers (blank delimited assumed) pulled from before the group. Under the circumstance that the file launches directly into numeric data, the header '(missing header)' will be used.
filename = 'abaqus.txt';
S = fileread(filename);
Slines = regexp(S, '(\r?\n)+', 'split');
mask = cellfun(@isempty, regexp(Slines, '^\s*\d', 'once'));
starts = strfind([1 mask], [1 0]);
stops = strfind([mask 1], [0 1]);
numgroups = length(starts);
group_headers = cell(numgroups, 1);
group_data = cell(numgroups, 1);
for G = 1 : numgroups
grouplines = Slines(starts(G):stops(G));
group_cols_cell = regexp( strtrim(grouplines), '\s+', 'split' );
group_data{G} = str2double( vertcat(group_cols_cell{:}) );
numcol = size(group_data{G},2);
if starts(G) == 1
group_headers{G} = {'(missing header)'}; %just in case no header
else
group_headers{G} = regexp(strtrim(Slines{starts(G)-1}), '\s+', 'split');
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!