MATLAB: Extract Variable from Filename

filegeneratevariable

Hi there,
I'm trying to write a reuseable function for a multitude of data points. My current filename when I download my data is:
sitename.yr.synth.daily.mat
Is there a straightforward way to extract the site name so that I can assign it to a column in the matrix? The year is already a column.
I will need to do this on hundreds of sites and years, so I don't want to code each site name by hand. I also need this so that I can match the site name with other classifications and generate unique file names.
Thanks a million!

Best Answer

You can use strtok() or other methods, but I prefer regexp here:
filename = 'sitename.yr.synth.daily.mat' %example
pieces = regexp(filename, '\.', 'split'); %divide by periods
sitename = pieces{1};