Loop for reading and extracting data from a single line of multiple text files.
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    James
 el 16 de Sept. de 2024
  
    
    
    
    
    Comentada: Star Strider
      
      
 el 17 de Sept. de 2024
            I have the following code that I would like to put into a loop to read multiple text files.
txt = fileread('Test01.txt');
  temppos = strfind(txt,'Ground Temperature:');
  endpos = strfind(txt, '°C');
    for k = 1:numel(temppos)
        section{k,:} = txt(temppos(k):endpos(k));
    end    for k = 1:numel(section)
        temp(k,:) = sscanf(section{k}, 'Ground Temperature: %f');
    end    Results = table(temp, 'VariableNames',{'Temp'});
    2 comentarios
Respuesta aceptada
  Star Strider
      
      
 el 16 de Sept. de 2024
        
      Editada: Star Strider
      
      
 el 16 de Sept. de 2024
  
      I generally do something like this — 
files = dir('*.txt');
for k = 1:numel(files)
    txt = fileread(files(k).name);
    temppos = strfind(txt,'Ground Temperature:');
    endpos = strfind(txt, '°C');
    for k = 1:numel(temppos)
        section{k,:} = txt(temppos(k):endpos(k));
    end
    for k = 1:numel(section)
        temp(k,:) = sscanf(section{k}, 'Ground Temperature: %f');
    end
    Results{k} = table(temp, 'VariableNames',{'Temp'});
end
Results{:}
This assumes the files are already in your working directory and on your MATLAB search path.  
If that is not the situation in your application, you will need something like this: 
files = dir('C:\PathName\*.txt')
and then: 
filename{k} = fullfile('C:\PathName\',files(k).name)
text = fileread(filename{k});
or something similar.  It may be neecessary for you to experiment a bit, depending on your situation.  
EDIT — Corrected typographical errors.  
.
13 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Structures 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!


