Extracting only few numeric columns from alphanumeric .out file.

3 views (last 30 days)
Hello there,
After searching for 2 days I could not find the solution myself. I finally have to post the question here to seek help from experts.
I have an .out file from the simulation software which contains the text of few pages, then a numeric table, and then 1 page of text.
I m interested in the numeric table which has 13357 rows and 10 columns. The numeric table looks like below.
Till now, i had 3 files like this. I manually deleted the text above and below the .out file and then imported to matlab manually selecting the desired columns and saved it as a matrix. But now i will be having lots of files like this so i need an optimized script.
I would like to extract only the numeric values for 1st, 2nd and 7th colums only. The desired output values are below, which i did manually.
I am attaching the .out file also.
  2 Comments
Ameer Hamza
Ameer Hamza on 8 Apr 2020
Does the outfile will always have the same number of header and footer rows?
Wajahat Farrukh
Wajahat Farrukh on 8 Apr 2020
The header and footer rows are not always the same.
but it always has "THETA", "PHI" etc on top and an empty row after the table is finished.
after empty row, there is a line statement which is always same.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 8 Apr 2020
try this
data = fileread('DJI3fq3.txt');
loc_nl = find(data==newline); % getting locations of all newlines
nl_double_nl = strfind(data, [newline newline]); % getting locations of all double newlines
loc_LOCATION = strfind(data, 'LOCATION'); % first column have title LOCATION
data_start = find(loc_nl > loc_LOCATION, 2); % 2nd newline after LOCATION is start of table data
data_start = loc_nl(data_start(2));
data_end = find(nl_double_nl > loc_LOCATION, 1); % 1st double newline after LOCATION is start of table data
data_end = nl_double_nl(data_end);
table_data = data(data_start+1:data_end-1); % extract data as char array
values = textscan(table_data, '%f%f%f%f%f%f%f%f%f%s'); % scan for numeric and string data
values = [num2cell([values{1:9}]) values{end}]; % cenvert to 2D cell array
T = cell2table(values); % convert to table
The values are stored in table T.
  2 Comments
Wajahat Farrukh
Wajahat Farrukh on 9 Apr 2020
exactly what i was looking for. Thanks alot brother.
i just made this file as a function to use in my scripts :)
very quick and to the point solution.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables 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!