Stuart’s MATLAB Videos

Watch and Learn

Advanced MATLAB: File name processing from directory listing 8

Posted by Doug Hull,

Very often MATLAB users want to be able to process a set of files from a directory. In this video, I show how you can look through a directory seeking files of the form “data###.dat” and then returning the “###” as a number with any leading zeros removed. This makes use of regular expressions, but does not explain their theory much at all.
  This is not the first time this kind of thing has been discussed. Check out Loren’s take, or Steve’s take from their blogs.

8 CommentsOldest to Newest

david replied on : 1 of 8
nice. just a thought: rather than trying ideas in the command window, one idea is to do this using the editor in cell mode. that way, when you've figured out the right syntax, you almost have a finished script. this also saves scrolling through the command history for the correct version of each command which after trial-and-error often look similar to versions that don't quite work regards, david
matt fig replied on : 2 of 8
Off topic, but David's reply made me think about an enhancement I would like to see. Imagine scrolling up through the command history, either through the editor or by using the up arrow. When you came across a command that caused an error, it would be in red font. Boy that would make life simpler when looking through several similar commands, some of which didn't work.
Doug replied on : 3 of 8
David, Actually, I tend to do a little bit of both. It is hard to say when I work at the command line and then script and when I write a script and just keep running it as you add to it. Doug
Doug replied on : 4 of 8
Matt, That is an excellent suggestion. I just posted it to our internal enhancement tracking database. You can make such requests more reliably by e-mailing support@mathworks.com Thanks, Doug
onassis replied on : 5 of 8
I saved a big(>2GB) .mat file on an external HD using save -v7.3 myfile.mat filetosave I try loading the file back with load myfile.mat and get an error message that file cannot be read. Any help?
dhull replied on : 6 of 8
@onassis Have you tried the procedure with a much smaller dataset to make sure you are doing everything right? If it works with a smaller dataset, then we can take it from there. Doug
Ana replied on : 7 of 8
My problem with reading from files is when i have a script like the one below and i want to asign to str a value that is actualy the file name. I want to do this as a command, not in the .m file function vect = citire(str) vect = zeros; i = 0; j = 0; f = fopen(str,'r'); while fscanf (f, '%d') ~= EOF i = i + 1; if mod(i,2) ~= 0 j = j + 1; vect(j) = fscanf(f,'%d'); end end disp(vect); fclose(str); end