- Is   cutTDM400_111_111_000_000_000   a sample name?
- Does   [1;33m--- Initializing LBM Domain   indicate the beginning of a "cell" ?
- I would use fileread and regexp
Extracting information from file
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to extract information from the attached file and write them into a matrix with one column each from sample name, number of cells and porosity. I have been trying textscan and sscanf, but am not sure how to search the structure of the text.
댓글 수: 3
per isakson
2016년 3월 8일
편집: per isakson
2016년 3월 8일
Now, I think I understand. The word "cells" in "Num of cells" has nothing to do with the word "cell" in "beginning of a cell is indicated".
First, I thought you wanted to count some kind of "sections" of the file. I missed "Num of cells = 32000000" when I first browsed the file.
채택된 답변
per isakson
2016년 3월 8일
편집: per isakson
2016년 3월 8일
This is one way to read the your file
>> tic, sas = nohup, toc sas = 1x1173 struct array with fields: SampleName NumOfCells Porosity Elapsed time is 27.000338 seconds. >> ix = find( strcmp( {sas.SampleName}, 'cutTDM050_111_121_221_222_122' ) ) ix = 583 >> sas(ix).Porosity ans = 0.0828 0.0828 0.0828 >> sas(ix).NumOfCells ans = 125000 125000 125000
where (in one m-file)
function sas = nohup %% str = fileread( 'nohup.txt' ); %% heading_string = 'Running Sample'; trailing_string = '=============================================='; % xpr = sprintf( '(?<=%s).+?(?=%s)', heading_string, trailing_string ); cac = regexp( str, xpr, 'match' ); %% sas = struct( 'SampleName',repmat({''},[1,length(cac)]) ... , 'NumOfCells',{[]}, 'Porosity', {[]} ); for jj = 1 : length( cac ) sas(jj) = nohup_( cac{jj} ); end end function sas = nohup_( str ) % sas.SampleName ... = regexp( str, 'cutTDM\d{3}_\d{3}_\d{3}_\d{3}_\d{3}_\d{3}', 'match', 'once' ); % cac = regexp( str, '(?<=Num of cells +\= *)\d+', 'match' ); sas.NumOfCells = str2double( cac ); % cac = regexp( str, '(?<=Porosity +\= *)[\d+\.]+', 'match' ); sas.Porosity = str2double( cac ); end
 
Comments:
The function is slow. Nearly all the time is spend with regexp searching for "Num of cells" and "Porosity". "the Num of cells and porosity value are the same." may be used improve speed. Adding 'once' to these two calls of regexp increases the speed forty times. That's much more than I anticipated; I don't understand; I cannot see what's taking all the extra time.
>> tic, sas = nohup, toc sas = 1x1173 struct array with fields: SampleName NumOfCells Porosity Elapsed time is 0.645206 seconds. >> ix = find( strcmp( {sas.SampleName}, 'cutTDM050_111_121_221_222_122' ) ) ix = 583 >> sas(ix).Porosity ans = 0.0828 >> sas(ix).NumOfCells ans = 125000 >>
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!