Read only numerical values from mixed text file
    1 view (last 30 days)
  
       Show older comments
    
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 Comments
Answers (1)
  dpb
      
      
 on 8 Jul 2019
        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 Comments
  dpb
      
      
 on 8 Jul 2019
				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.
See Also
Categories
				Find more on Text Data Preparation 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!

