Read only numerical values from mixed text file
    조회 수: 1 (최근 30일)
  
       이전 댓글 표시
    
Hi,
I have a text file with two matrices. One contains Nodes No., X,Y,Z values and the other contains Bar No., Start , End values. I have text before and between them. I would like to create a general code to extract those two matrices no matter the size. Please see file attached.
Thank you in advance.
Regards,
Ana
댓글 수: 0
답변 (1개)
  dpb
      
      
 2019년 7월 8일
        Something like
fmt1='NODes %n  ELEments %n';         % info line nodes, elements in file
fmt2=repmat('%f',1,4);
fmt3=repmat('%f',1,3);
fid=fopen('yourfile.txt','r');
nums=textscan(fid,fmt1),1,'headerlines',9);
nNodes=nums{1};
nElems=nums{2};
NODES=cell2mat(textscan(fid,fmt2),nNodes,'headerlines',8));
BARS=cell2mat(textscan(fid,fmt3),nElems,'headerlines',8));
fid=fclose(fid);
댓글 수: 2
  dpb
      
      
 2019년 7월 8일
				Ewww...looking at the file, it's some multi-byte format and Matlab has problems with those.
I'm sure there's probably a way to deal with it, but I don't have the time right now to try to do so, sorry...
fgetl for the header lines looks like--
>> for i=1:20,l=fgetl(fid),strfind(l,'NODes'),end
l =
    '��; + - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - + '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' ; !   F i l e   N a m e :     T - W h a r f _ B e r t     !   D a t e :   0 7 / 0 2 / 1 9         1 4 : 1 0   !   R O B O T   9 7   v . 3 2 . 0           ! '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' ; + - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - + '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' R O B O T 9 7 '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' S H E l l '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' N U M b e r i n g   D I S c o n t i n u o u s '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' '
ans =
     []
l =
    ' N O D e s   2 0 4 4     E L E m e n t s   1 7 1 7 '
ans =
     []
l =
    ' '
ans =
     []
>> 
so the string matching fails as well as the line count from just looking at the file.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


