How to exclude the characters row in a text file?

4 ビュー (過去 30 日間)
zainab malik
zainab malik 2018 年 12 月 31 日
コメント済み: zainab malik 2019 年 1 月 1 日
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 件のコメント
zainab malik
zainab malik 2018 年 12 月 31 日
Thank you for the comment. Yes its necessary to read all of the number grounds they might be different if I add more results. It's an example problem. They should be stored independently.. As, they are results from different jobs. Kindly help!
zainab malik
zainab malik 2018 年 12 月 31 日
I am trying to use dlmread command it do read the values of first job but for second values(results) its giving error because of string at 84th row

サインインしてコメントする。

採用された回答

Walter Roberson
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
  1 件のコメント
zainab malik
zainab malik 2019 年 1 月 1 日
Thankyou so much. It's working perfectly fine :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by