Friday, June 29, 2012

How to read files available in a current directory and read their name in Matlab?

Imagine a directory contains some image files named ref1.png ref2.png ......and test1.png test2.png...........

We want to print the names of these files available in C:\Users\code

dir_loc='C:\Users\code';



ref_files=dir( strcat(dir_loc,'ref*.png'));
test_files=dir(strcat(dir_loc,'test*.png'));


for i=1:numel(ref_files)

    ref_files(i).name
    test_files(i).name

end


The above code will print all the files which name's contain either ref or test string from the "code" directory.

2 comments:

  1. when dealing with file names and paths, it is usually better to use "fullfile", rather than "strcat".

    ReplyDelete
  2. Yes. That's a good option as well.

    ReplyDelete

Please ask if anything is not clear enough..........