matlab - loop through all files in a given folder in Unix

In matlab, if you want to process all files in folder and the files have rather different file names you can use following code snippet in Unix environments, like linux
 

%// use ls command to get a long string of all files you are interested in
[status,cmdout] = unix('ls /path/to/directory/*filesMustContainThisWord*');

%// convert that long string to cells, 
%// using the end of line character as the splitting point 
%// note: in other operating systems \r or \f
%//  or a combination might be the EOL character 
cmdout_cells = regexp(cmdout, '\n', 'split');

% // loop through all file names, last cell is empty ''
for i = 1 : length(cmdout_cells)-1
  fprintf('%s \n', cmdout_cells{i}); %// just prints the file names
end

 

 

blog_tags: 

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <img> <br> <p> <span>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Code snippets in <code>...</code> or <source>...</source> automatically will be pretty printed.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image