File operations‎ > ‎

File commands



create directory, if not exit
dirname = 'NewDirectory'
if ~isdir(dirname), mkdir(dirname); end 
  

get filename (sample) list of a directory
dir('*.csv')


read all files of a directory
fileinfo = dir('/path/to/my/folder/');      % get info (name,size,date,..) for all files in my folder
filelist = {fileinfo(:).name}';                          % get all filenames
filelist(ismember(filelist,{'.','..'}))=[]; % remove '.' and '..' directory names

filelist
'file1.txt'
'file2.txt'
'file3.txt'





read all '.csv' files and extract samplenames

fileFormat = '.csv'
dirSamples = '/path/to/data/'
 sdir = dir([dirSamples,'*',fileFormat]) 
% struct info of directory
 numSamples = numel(sdir);
 SampleFilename = cell(numSamples,1);
 SampleIDs = cell(numSamples,1);
  for i=1:numSamples
   SampleFilename{i} = sdir(i).name;
   SampleIDs{i} = strrep(SampleFilename{i},fileFormat, ''); % remove ending '.csv'
  end

SampleFilename
SampleIDs
 




Comments