extract numbers or string after specific symbol from a text file
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Binu
 el 6 de Ag. de 2020
  
    
    
    
    
    Comentada: Binu
 el 9 de Ag. de 2020
            Hello
I have a set of text files with different attributes.  Here is only three attributes of one file for example;
Location ID : 600
Field code : RNW
Sample interval : 60
The line number of these attributes are not the same for each file. How can I extract values after  Location ID :, Field code :, and Sample interval :  from all my text files if I put them in a loop.
Thank you
0 comentarios
Respuesta aceptada
  Walter Roberson
      
      
 el 6 de Ag. de 2020
        
      Editada: Walter Roberson
      
      
 el 6 de Ag. de 2020
  
      dinfo = dir('*.txt');
nfiles = length(dinfo);
filenames = fullfile({dinfo.folder}, {dinfo.name});
IDs = cell(nfiles,1);
FCs = cell(nfiles,1);
intervals = cell(nfiles,1);
for K = 1 : nfiles
    filename = filenames{K};
    S = fileread(filename);
    parts = regexp(S, '(?<=Location ID\s*:\s*)(?<ID>\d+).*(?<=Field code\s*:\s*)(?<FC>\w+).*(?<=Sample interval\s*:\s*)(?<SI>\w+)', 'names');
    IDs{K} = str2double({parts.ID});
    FCs{K} = {parts.FC};
    intervals{K} = str2double({parts.SI});
end
Note: this code does not assume that there is only one occurance of the information per file. It will find all of the occurance in each file, provided that the parts are all in the same order and none of the parts are missing.
If there is only ever one occurance then the code could be simplified a little.
3 comentarios
  Walter Roberson
      
      
 el 8 de Ag. de 2020
				dinfo = dir('*.txt');
nfiles = length(dinfo);
filenames = fullfile({dinfo.folder}, {dinfo.name});
IDs = zeros(nfiles,1);
FCs = cell(nfiles,1);
intervals = zeros(nfiles,1);
for K = 1 : nfiles
    filename = filenames{K};
    S = fileread(filename);
    parts = regexp(S, '(?<=Location ID\s*:\s*)(?<ID>\d+).*(?<=Field code\s*:\s*)(?<FC>\w+).*(?<=Sample interval\s*:\s*)(?<SI>\w+)', 'names', 'once');
    IDs(K) = str2double(parts.ID);
    FCs{K} = parts.FC;
    intervals(K) = str2double(parts.SI);
end
Más respuestas (0)
Ver también
Categorías
				Más información sobre MATLAB Report Generator 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!

