In the past we covered how to use UIGETFILE as an alternative to hardcoding the name of a file in your program. [click here] In this video, we will show how MATLAB can get a directory listing and use it to batch process the files in a directory.
Hello,
I know that it is off topic but I hope I can an answer. Which software do you use to create these videos? They are very well created and they are very informative.
Wow! I work in computational psychology and really prefer to keep my subject’s data in individual .mat files with their subject numbers. Until now, when I aggregated the data I would physically move copies of the .mat files into new directories. Now I can keep them all in one and select them in a loop based on certain criteria. Awesome! You just saved me weeks of work!
Thanks for letting me know. We like to hear that these blogs are helpful to people. Be sure to subscribe to this, and the other MathWorks blogs for more great ideas!
Thank you for the tutorial but I must ask, once you have the files required in the array, how do you parse (read) them? You seemed to have a generic function to do it bit I am unsure how to extract the name from the array to read the data in.
Hi
I have a list of folders and inside each folder i have a list of tif files which I have to process. I am now able to read all the tif files using a combination of ‘dir’ command and ‘imread’ command in a m-file.
My intention is to go into first folder, read all the tif files, process them, then come out. Then I will have to read the second folder, read all the tif files and process them and so on. Can you tell me how to open a folder and come out of a folder ? Basically I need an alternative for cd command to be used in a m-file.
The command dir is useful for the current directory.
for example, If I have to open a file which is not in the current directory but located at some path ‘D:\abc\’, ‘D;\abcd’ and of type .tif
now using the code
a = dir(‘D:\abc\*.tif’) or a = dir(‘D:\abcd\*.tif’)
a(1).name only returns the name of the file and not the path
I will have to append the path with the name using strcat to open it.
I think fileattrib will be more useful here
the following code works for the above problem by sundar
[b, a] = fileattrib('D:\abc\*.tif');
if(b)
S = size(a)
for i = 1:S(1,2)
imread(a(i).Name);
end
end
[b, a] = fileattrib('D:\abcd\*.tif');
if(b)
S = size(a)
for i = 1:S(1,2)
imread(a(i).Name);
end
end
sundar you can go with the above code
Hello,
I know that it is off topic but I hope I can an answer. Which software do you use to create these videos? They are very well created and they are very informative.
Best regards
Mans
Mans,
I use Camtasia.
http://www.techsmith.com/camtasia.asp
Doug
Wow! I work in computational psychology and really prefer to keep my subject’s data in individual .mat files with their subject numbers. Until now, when I aggregated the data I would physically move copies of the .mat files into new directories. Now I can keep them all in one and select them in a loop based on certain criteria. Awesome! You just saved me weeks of work!
Russell,
Thanks for letting me know. We like to hear that these blogs are helpful to people. Be sure to subscribe to this, and the other MathWorks blogs for more great ideas!
-Doug
Hey,
Thank you for the tutorial but I must ask, once you have the files required in the array, how do you parse (read) them? You seemed to have a generic function to do it bit I am unsure how to extract the name from the array to read the data in.
Thanks
Ben,
The name is in the name field as you saw in the video:
files(1).name
will give you just the name of the first file.
Depending on the files you are reading, you would use different functions, like CSVREAD, XLSREAD, IMREAD
What kind of files are you trying to read?
Doug
Hi
I have a list of folders and inside each folder i have a list of tif files which I have to process. I am now able to read all the tif files using a combination of ‘dir’ command and ‘imread’ command in a m-file.
My intention is to go into first folder, read all the tif files, process them, then come out. Then I will have to read the second folder, read all the tif files and process them and so on. Can you tell me how to open a folder and come out of a folder ? Basically I need an alternative for cd command to be used in a m-file.
Thanks
sundar
The command dir is useful for the current directory.
for example, If I have to open a file which is not in the current directory but located at some path ‘D:\abc\’, ‘D;\abcd’ and of type .tif
now using the code
a = dir(‘D:\abc\*.tif’) or a = dir(‘D:\abcd\*.tif’)
a(1).name only returns the name of the file and not the path
I will have to append the path with the name using strcat to open it.
I think fileattrib will be more useful here
the following code works for the above problem by sundar
[b, a] = fileattrib('D:\abc\*.tif');if(b)
S = size(a)
for i = 1:S(1,2)
imread(a(i).Name);
end
end
[b, a] = fileattrib('D:\abcd\*.tif');
if(b)
S = size(a)
for i = 1:S(1,2)
imread(a(i).Name);
end
end
sundar you can go with the above code
Thanks a lot Doug !
Your example also works with directories !
The naming system of this function is an epic fail.
For example it takes as 2190 is smaller than 920. Because it is starting with 2!
I’ve been trying to list only the folders in the path, that is to say, I want to exclude files and just list the folders.
I have been able to do this using the unix command line from matlab using:
[Status,Folders]=unix(‘ls -d */| grep -v private’);
However, I would like my scripts to be portable to windows systems so I would like to find a native solution in matlab.
Any ideas?
Thanks in advance,
@Anne,
You can take advantage of the DIR function which lists all files/folders in the directory and eliminate the non directories:
x = dir; justdirs = x([x.isdir]) dirnames = {justdirs.name}my mmreader is not working also the problem is aviread is giving warnings and not playing larger average quality video
I would like to find tiff images within several folders. Once located, I want to copy them in a new directory. Is there a short way to do this?
Hi,
I have a simple question, I would like to extract the directory name from the file path given.
For eg: getDir(‘../testDir/test.dat’);
i want to get back only the dir name in this case ‘../testDir/’.
Is there a way to do it in matlab.