How to exclude the characters row in a text file?
조회 수: 4 (최근 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!