Excluding files with a certain keyword in them
    10 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Ibro Tutic
      
 am 4 Nov. 2015
  
    
    
    
    
    Kommentiert: Rena Berman
    
 am 6 Mai 2021
            I am trying to get data from many CSV files, some of which are unneeded. These unneeded files all contain the word Composite (specifically _X0_00XX0_Composite.csv where X is a placeholder for some letter and 0 is a place holder for some number). Is there a way to tell my program to not attempt to read these files? The data is not needed and is not formatted for my program and I run into issues when trying to read the data. I assume it would be some sort of if statement, where it checks the file name for 'composite' and if true, I need it to break and continue scanning the other files. I assume I would need to put this piece of code as the first thing in the 3rd for loop.
 clear all
 close all
 clc
 %%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 projectdir = '\\frg1\shared\PerformanceCalibration\FieldPerformance\SAR  Data\LoadProfile\SIM_LoadProfile_Dev\Processed\';    %Start here. or name an absolute directory
 newdir = 'C:\Users\it58528\Desktop\Test';
 folderinfo = dir(projectdir);
 folderinfo = folderinfo([folderinfo.isdir]);  %select only the directories
 folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'}));  %remove directories . and ..
 %%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
 for folderidx = 1 : length(folderinfo)
   thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
   subfolderinfo = dir(thisfolder);
   subfolderinfo = subfolderinfo([subfolderinfo.isdir]);  %select only the directories
   subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'}));   %remove directories . and ..
   folderidxi = folderinfo(folderidx).name;
   newfolder = fullfile(newdir, folderidxi);
   mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
 subfolderi  = subfolderinfo(subfolderidx).name;
 thissubfolder = fullfile(thisfolder, subfolderi);
 fileinfo = dir( fullfile(thissubfolder, '*.csv') );
   newsubfolder = fullfile(newfolder, subfolderi);
   mkdir(newsubfolder);
    for fileidx = 1 : length(fileinfo)
        filenamei = fileinfo(fileidx).name;
        thisfile = fullfile(thissubfolder, filenamei);
        [filepath, basename] = fileparts(thisfile);
        data = csvread(thisfile,5,2);
        fileID = fopen(thisfile);
        C=textscan(fileID,'%s %s %s %s');
        fclose(fileID);
        filename=fileinfo(fileidx).name(1:13);
        PIN(fileidx).PIN = fileinfo(fileidx).name(1:13);
        PIN(fileidx).serialnumber = C{1,4}{2,1};
        PIN(fileidx).loadprofile = data(1:15,:);
        PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
        PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
        PIN(fileidx).loadpercent = data(:,2);
        PIN(fileidx).RPM = data(16,:);
        loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
        xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
        newfilename = fullfile(newsubfolder, filename);
        save(newfilename,'PIN'); %%%%FIX
    end   %files within subfolder
end     %subfolders within folder
 end       %folders
2 Kommentare
  Stephen23
      
      
 am 26 Dez. 2020
				Original question by Ibro Tutic on 4th of November 2015 retrieved from Google Cache:
Excluding files with a certain keyword in them
I am trying to get data from many CSV files, some of which are unneeded. These unneeded files all contain the word Composite (specifically _X0_00XX0_Composite.csv where X is a placeholder for some letter and 0 is a place holder for some number). Is there a way to tell my program to not attempt to read these files? The data is not needed and is not formatted for my program and I run into issues when trying to read the data. I assume it would be some sort of if statement, where it checks the file name for 'composite' and if true, I need it to break and continue scanning the other files. I assume I would need to put this piece of code as the first thing in the 3rd for loop.
 clear all
 close all
 clc
 %%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 projectdir = '\\frg1\shared\PerformanceCalibration\FieldPerformance\SAR  Data\LoadProfile\SIM_LoadProfile_Dev\Processed\';    %Start here. or name an absolute directory
 newdir = 'C:\Users\it58528\Desktop\Test';
 folderinfo = dir(projectdir);
 folderinfo = folderinfo([folderinfo.isdir]);  %select only the directories
 folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'}));  %remove directories . and ..
 %%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
 for folderidx = 1 : length(folderinfo)
   thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
   subfolderinfo = dir(thisfolder);
   subfolderinfo = subfolderinfo([subfolderinfo.isdir]);  %select only the directories
   subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'}));   %remove directories . and ..
   folderidxi = folderinfo(folderidx).name;
   newfolder = fullfile(newdir, folderidxi);
   mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
 subfolderi  = subfolderinfo(subfolderidx).name;
 thissubfolder = fullfile(thisfolder, subfolderi);
 fileinfo = dir( fullfile(thissubfolder, '*.csv') );
   newsubfolder = fullfile(newfolder, subfolderi);
   mkdir(newsubfolder);
    for fileidx = 1 : length(fileinfo)
        filenamei = fileinfo(fileidx).name;
        thisfile = fullfile(thissubfolder, filenamei);
        [filepath, basename] = fileparts(thisfile);
        data = csvread(thisfile,5,2);
        fileID = fopen(thisfile);
        C=textscan(fileID,'%s %s %s %s');
        fclose(fileID);
        filename=fileinfo(fileidx).name(1:13);
        PIN(fileidx).PIN = fileinfo(fileidx).name(1:13);
        PIN(fileidx).serialnumber = C{1,4}{2,1};
        PIN(fileidx).loadprofile = data(1:15,:);
        PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
        PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
        PIN(fileidx).loadpercent = data(:,2);
        PIN(fileidx).RPM = data(16,:);
        loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
        xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
        newfilename = fullfile(newsubfolder, filename);
        save(newfilename,'PIN'); %%%%FIX
    end   %files within subfolder
end     %subfolders within folder
 end       %folders
Akzeptierte Antwort
  TastyPastry
      
 am 4 Nov. 2015
        
      Bearbeitet: TastyPastry
      
 am 4 Nov. 2015
  
      Assuming you have the file name as a variable, I'll use myName:
myName = lower(myName);
idx = strfind(str,'composite');
if ~isempty(idx)
  %do nothing
else
  %do something
end
You need to find if 'Composite' is in the file name. strfind() returns the indices where it finds the match as a vector, if it doesn't find anything, it returns the empty vector. If you check whether or not the vector is empty, you can check whether or not 'Composite' is in the file name. I used lower() because strfind() is case sensitive. If case matters, you can just ignore the line
myName = lower(myName);
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



