How to parse such data pattern from a text file

2 views (last 30 days)
Hello,
i have trying to extract such data pattern below with no success so far:
FOSW
0.045 0 0.49885 0.955
0.184 0 0.24282 0.816
0.20512 3.4639E-005 0.2125 0.79488
0.22624 0.00027712 0.18437 0.77376
0.24735 0.00093526 0.15841 0.75265
0.26847 0.0022169 0.13457 0.73153
0.28959 0.0043299 0.11285 0.71041
0.31071 0.0074821 0.093186 0.68929
0.33182 0.011881 0.075559 0.66818
0.35294 0.017735 0.059927 0.64706
0.37406 0.025252 0.046247 0.62594
0.39518 0.034639 0.034475 0.60482
0.41629 0.046105 0.02456 0.58371
0.43741 0.059857 0.016445 0.56259
0.45853 0.076103 0.010065 0.54147
0.47965 0.095051 0.0053451 0.52035
0.50076 0.11691 0.0021906 0.49924
0.52188 0.14188 0.00047675 0.47812
0.543 0.17018 0 0.457
1 1 0 0
/
0 0 0.49885 0
0.105 0 0.35156 0
0.14082 1.1039E-005 0.30766 0
0.17665 0.0001249 0.26694 0
0.21247 0.00051626 0.22934 0
0.24829 0.001413 0.19484 0
0.28412 0.0030856 0.16338 0
0.31994 0.0058409 0.13492 0
0.35576 0.010018 0.1094 0
0.39159 0.015987 0.086764 0
0.42741 0.024143 0.066958 0
0.46324 0.03491 0.049914 0
0.49906 0.048733 0.035558 0
0.53488 0.066082 0.023809 0
0.57071 0.087448 0.014573 0
0.60653 0.11334 0.0077388 0
0.64235 0.1443 0.0031716 0
0.67818 0.18087 0.00069025 0
0.714 0.22362 0 0
0.955 1 0 0
/
it consist two datasets, each one is terminated by / . from FOSW to the first /, this is the first dataset. from 1st / to the 2nd /, this is the 2nd dataset.
each dataset contains 4 column and could be variable number of rows. there could be more the two datasets.
please advise

Answers (1)

Ronit
Ronit on 19 Sep 2024
Hello Ahmed,
To extract the datasets from the text pattern you've provided, you can use regular expressions and text processing functions in MATLAB. Firstly, store the data in string format, use regular expressions to split the data based on the "/" delimiter then convert each dataset into a matrix by reading the numbers.
dataString = [
"FOSW"
" 0.045 0 0.49885 0.955"
" 0.184 0 0.24282 0.816"
" /"
" 0 0 0.49885 0"
" 0.105 0 0.35156 0"
" /"
];
% Combine the lines into a single string
dataString = join(dataString, newline);
datasets = split(dataString, '/');
% Initialize a cell array to store parsed datasets
parsedData = {};
for i = 1:length(datasets)
% Trim whitespace and split lines
lines = strtrim(split(datasets{i}, newline));
% Initialize a matrix to store numbers
dataMatrix = [];
% Loop through each line
for j = 1:length(lines)
line = lines{j};
% Skip empty lines or lines with non-numeric characters
if isempty(line) || any(isletter(line))
continue;
end
% Convert the line into a row vector of numbers
numbers = sscanf(line, '%f');
% Append the row to the data matrix
dataMatrix = [dataMatrix; numbers'];
end
% Store the parsed data matrix
if ~isempty(dataMatrix)
parsedData{end+1} = dataMatrix;
end
end
% Display the parsed datasets
for i = 1:length(parsedData)
fprintf('Dataset %d:\n', i);
disp(parsedData{i});
end
Dataset 1:
0.0450 0 0.4989 0.9550 0.1840 0 0.2428 0.8160
Dataset 2:
0 0 0.4989 0 0.1050 0 0.3516 0
Please go through the following MATLAB documentation links for further information:
  1. “split” - https://www.mathworks.com/help/matlab/ref/string.split.html
  2. “join” – https://www.mathworks.com/help/matlab/ref/join.html
  3. strtrim” - https://www.mathworks.com/help/matlab/ref/strtrim.html
I hope this resolves your query!

Products

Community Treasure Hunt

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

Start Hunting!