Reading data of nonstandard format
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Mohammad Farhat
 el 24 de Sept. de 2021
  
    
    
    
    
    Comentada: Star Strider
      
      
 el 24 de Sept. de 2021
            Hello, 
The data file I'm trying to read contains text headers separating two column matricies that I want to save separately.
Each header is over several lines (different number for each header), all header lines start with ">", then I have the two column matrix space separated. 
a reduced sample of this file is attached. Please let me know how to read it and save the matricies separately.
Thanks
0 comentarios
Respuesta aceptada
  Star Strider
      
      
 el 24 de Sept. de 2021
        I use textscan for these problems.  
Try this — 
fidi = fopen('reconstructed[1].txt','rt')
k1 = 1;
while ~feof(fidi)
    HL = 14*(k1==1) + 10*(k1>1);
    C = textscan(fidi, '%f%f', 'HeaderLines',HL, 'CollectOutput',true);
    M = cell2mat(C);
    if isempty(M)                                       % Empty Matrix Indicates End-Of-File
        break
    end
    D{k1,:} = M;
    fseek(fidi, 0, 0);
    k1 = k1 + 1;
end
D
D_Check_1 = D{1}(1:5,:)                         % First 5 Rows
D_Check_3 = D{3}(1:5,:)                         % First 5 Rows
Out = cell2mat(D)
The different segments import correctly.  The rest of the file should as well, so long as the file format and separating lines remain the same.  If those change, it will be necessary for you to tweak the code to adapt it to them.  
Here, ‘D’ has the individual matrices, and ‘Out’ has them vertically concatenated.  
.
3 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Environment and Settings en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

