MATLAB Answers

How to ignore headers and select specific rows of Data to be improrted from text file.

조회 수: 5(최근 30일)
Amir Ahmadi
Amir Ahmadi 22 Aug 2018
편집: jonas 22 Aug 2018
Hi, I'm trying to do some post processing from openfoam with matlab but the data I want to import into matlab has headers also some numbers and strings mixed in between I just want to import the numeric values into a nx3 matrix. I'm able to do it interactively, but I need to import data programmatically because I have numerous data to import. I tried using texscan but I don't know where can I determine which rows I want to be imported in this command. is the textscan the only option to import such data and how?
Note: a sample of my data is shown below and the full data is attached.
Thank very much in advance for your help
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 5.x |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------
FoamFile
{
version 2.0;
format ascii;
class volVectorField;
location "3000";
object U;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 1 -1 0 0 0 0];
internalField nonuniform List<vector>
10000
(
(-6.27159e-06 6.10238e-06 0)
(-8.42477e-05 2.23045e-05 0)
(-0.000226515 3.24013e-05 0)
(-0.00040871 3.60096e-05 0)
(-0.00060764 3.67297e-05 0)
(-0.000812439 3.64566e-05 0)
(-0.00101913 3.61134e-05 0)
(-0.00122682 3.60697e-05 0)
(-0.00143574 3.6378e-05 0)
(-0.00164629 3.69498e-05 0)
(-0.00185851 3.76546e-05 0)
(-0.00207198 3.83631e-05 0)
boundaryField
{
adiabaticWalls
{
type fixedValue;
value uniform (0 0 0);
}
(-0.000226515 3.24013e-05 0)
(-0.00040871 3.60096e-05 0)
(-0.00060764 3.67297e-05 0)
(-0.000812439 3.64566e-05 0)
(-0.00101913 3.61134e-05 0)
// ************************************************************************* //

  댓글 수: 0

로그인 to comment.

채택된 답변

jonas
jonas 22 Aug 2018
편집: jonas 22 Aug 2018
You can try this (probably overly complicated) solution using regular expressions
%%Read file
str=fileread('File.txt');
%%Remove some annoying segments
str=regexprep(str,'(0 0 0)',' ');
%%Find data in brackets and remove brackets
n='(+|-)?\d+(\.\d+)?(e(+|-)?\d+)?';
str_out=regexp(str,['[(]',n,'\s',n,'\s',n,'[)]'],'match');
str_out=regexprep(str_out, '(', '')';
str_out=regexprep(str_out, ')', '');
num_out=cellfun(@str2num,str_out,'UniformOutput',false);
num_out=cell2mat( num_out )
ans =
1.0e-05 *
-0.0001 0.0001 0
-0.0011 0.0006 0
-0.0054 0.0021 0
-0.0156 0.0050 0
-0.0306 0.0079 0
-0.0473 0.0092 0
-0.0626 0.0087 0
-0.0894 0.0014 0

  댓글 수: 0

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

제품


릴리스

R2018a

Translated by