Read data from a complex file
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Research
 el 26 de Sept. de 2023
  
    
    
    
    
    Comentada: Voss
      
      
 el 28 de Sept. de 2023
            Hi Help,
I'm trying to read the data from this kind of file. Here the headers repeat randomly at various rows and first column data is date of the year. I tried using load and readmatrix functions, but no luck. Any thoughts?
Data file has four columns. First column data is day (mm-dd-yy). Header labels from each row repeats across the file but not periodically.
header  header2  header3  header4    
4-25-16  5  6  7    
4-25-16  24  2  25    
header  header2  header3  header4    
4-25-16  52  62  72    
4-25-17  2  24  2    
4-25-18  52  62  72   
 4-25-19  25  26  28    
header  header2  header3  header4   
4-25-19  52  62  72    
header  header2  header3  header4   
 4-25-19  52  62  72    
4-25-20  2  24  2    
4-25-21  52  62  72    
4-25-22  25  26  28    
4-25-23  5  6  7    
4-25-24  24  2  25    
header  header2  header3  header4    
4-25-24  5  6  7    
4-25-25  24  2  25 
0 comentarios
Respuesta aceptada
  Voss
      
      
 el 26 de Sept. de 2023
        
      Editada: Voss
      
      
 el 26 de Sept. de 2023
  
      Something like the following may work. You may need to change the header_str and text_scan_format to match your actual file's contents.
% path to the file:
file_name = 'file.txt';
% lines in the file starting with header_str will be skipped:
header_str = 'header';
% format of data lines:
text_scan_format = '%d-%d-%d %d %d %d';
% show file's contents, for reference:
type(file_name);
% initialize an empty datetime array dt and an empty 3-column matrix vals:
dt = NaT(0,1);
vals = zeros(0,3);
% open the file for reading:
fid = fopen(file_name,'r');
% while not at the end of the file:
while ~feof(fid)
    % get the next line:
    str = fgetl(fid);
    % if it starts with header_str, skip it:
    if startsWith(str,header_str)
        continue
    end
    % read the date and other values from the line:
    temp = textscan(str,text_scan_format);
    % put the date in a new elements in the dt array:
    dt(end+1,:) = datetime(temp{[3 1 2]}); % year, month, day
    % put the other values in a new row in the vals matrix:
    vals(end+1,:) = [temp{4:6}];
end
% close the file:
fclose(fid);
% put dt and vals together in a table:
T = addvars(array2table(vals),dt,'Before','vals1')
2 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Text Files en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

