How to exclude the characters row in a text file?
3 views (last 30 days)
Show older comments
zainab malik
on 31 Dec 2018
Commented: zainab malik
on 1 Jan 2019
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 Comments
Accepted Answer
Walter Roberson
on 1 Jan 2019
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
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!