How to exclude the characters row in a text file?

3 visualizaciones (últimos 30 días)
zainab malik
zainab malik el 31 de Dic. de 2018
Comentada: zainab malik el 1 de En. de 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 comentarios
zainab malik
zainab malik el 31 de Dic. de 2018
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 el 31 de Dic. de 2018
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

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de En. de 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

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by