Extracting only few numeric columns from alphanumeric .out file.
조회 수: 2 (최근 30일)
이전 댓글 표시
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.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282880/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282880/image.jpeg)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282881/image.jpeg)
I am attaching the .out file also.
댓글 수: 2
Ameer Hamza
2020년 4월 8일
Does the outfile will always have the same number of header and footer rows?
채택된 답변
Ameer Hamza
2020년 4월 8일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!