how to extract data from directory
    12 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    sam moor
 el 3 de En. de 2017
  
    
    
    
    
    Comentada: Walter Roberson
      
      
 el 19 de En. de 2017
            I have main directory and 20 sub directories in main directory. In each sub directories there are 4 files. Now, I would like to extract those 4 files from each sub directories in matlab files so that I can do manipulation for each file without doing manually. Is there any suggestion for this?
0 comentarios
Respuesta aceptada
  Walter Roberson
      
      
 el 4 de En. de 2017
        project_dir = pwd();     %or give a particular directory name
mainDirContents = dir(project_dir);
mainDirContents(~[MainDirContents.isdir]) = [];   %remove non-folders
mask = ismember( {MainDirContents.name}, {'.', '..'} );
mainDirContents(mask) = [];                       %remove . and .. folders
num_subfolder = length(mainDirContents);
for subfold_idx= 1 : num_subfolder
  this_folder = fullfile( project_dir, mainDirContents(subfold_idx).name );
    fprintf('Now working with folder "%s"\n', this_folder );
    at2Contents = dir( fullfile(this_folder, '*.AT2') );
    num_at2 = length(at2Contents);
    for at2idx = 1 : num_at2
       this_at2 = fullfile( this_folder, at2Contents(at2idx).name );
       now do something with the file whose complete name is given by this_at2
    end
  end
7 comentarios
  Walter Roberson
      
      
 el 19 de En. de 2017
				Are your subdirectories numbered 1 to 22? Or do they have the names given? Those names include slashes, and slashes mark directory delimiters, so you cannot be directly using those names.
  Walter Roberson
      
      
 el 19 de En. de 2017
				num = xlsread('NormalizationFactors.xls');
norm_factors = num(:,end);
subfolder_results = cell(num_subfolder, 1);
for subfold_idx = 1 : num_subfolder
  subfolder_name = mainDirContents(subfold_idx).name;
  this_folder = fullfile( project_dir, subfolder_name );
  fprintf('Now working with folder "%s"\n', this_folder );
    folder_id = sscanf(subfolder_name, '%d', 1);
    this_norm_value = norm_factors(folder_id);
    at2Contents = dir( fullfile(this_folder, '*.AT2') );
        num_at2 = length(at2Contents);
        each_file_results = cell(num_at2, 1);
        for at2idx = 1 : num_at2
           this_at2 = fullfile( this_folder, at2Contents(at2idx).name );
           at2_content = ReadAT2File( this_at2 );  %might as well put it inside a function
           each_file_results{at2idx} = this_norm_value * at2_content;
        end
        subfolder_results{subfold_idx} = each_file_results;
      end
Más respuestas (1)
  Geoff Hayes
      
      
 el 3 de En. de 2017
        sam - use dir to get an array of the sub-directories (from the main directory). Then iterate over each element in this array using isdir to ensure that it is a directory. If so, then again use dir on this sub-directory to get an array of files within this sub-directory. Iterate over this array, and perform whatever manipulation that you need for the file depending upon its type. See Data Import and Export for details.
6 comentarios
  Geoff Hayes
      
      
 el 4 de En. de 2017
				sam - as Walter has shown below, use a filter with dir to find all those files that have an extension of AT2.
Ver también
Categorías
				Más información sobre File Operations 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!



