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.
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
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
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.
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
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
Hi
I’m trying to get the number from my filename, and I’m doing exactly as shown in the video, but when I type ans(1) my answer is 1×1 cell.
Do you know what I’m doing wrong?
/Kaija
My filename is tpjp199202.mon and my commands are:
files=dir(‘*.mon’);
name=files(1).name;
regexp(name,’tpjp(\d*).mon’,'tokens’);
ans
ans =
{1×1 cell}
ans(1)
ans =
{1×1 cell}
@Kaija,
Use {} not ()
Doug
Hello guys
Do you have any idea of how can I list files from the web site?
for example, I have many images that have this form 20110101_0000_c2_512.jpg to be processed the only thing changes is 20110101_THIS_c2_512.jpg and it has to be 4 digits start from 0000 to 2354
this is the website
http://sohowww.nascom.nasa.gov//data/REPROCESSING/Completed/2011/c2/20110101/
I want to list these files, just the image files.
your help is appreciated in advance
Doug, a small correction. ‘*’ in regular expressions means 0 or more. It is ‘+’ that mean 1 or more. So if we have a file ‘data.dat’, it will also match your regexp returning empty string as a token.
By the way, if someone need just a list of filenames, it can be done in several ways without for-loop.
namelist = ls('*.dat'); % as a char array namelist = cellstr(ls('*.dat')); % as a cell array of strings namelist = {files(:).name}'; % from dir outputHow can i store data from txt files onto a string in matlab? I have a bunch of dat files that serve as a database,and they have to be compared to an input file(text file) .
This is the code snippet i’ve managed to come up with but the importdata line shows an error sayin fullfile cannot be used for input of type struct. a holds a 5*1 structure
g=fileread(‘e:\…’);
f=['e:' filesep 'dfiles'];
a=dir(f);
data=zeros(size(g),15);
for i=1:5
data(i)=importdata(fullfile(f,a(i)));
end