Extract Variable from Filename?
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Matt Kindig
2013-5-29
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};
更多回答(1 个)
Azzi Abdelmalek
2013-5-29
编辑:Azzi Abdelmalek
2013-5-29
file='sitename.yr.synth.daily.mat'
idx=strfind(file,'.')
site=file(1:idx(1)-1)
%or
file='sitename.yr.synth.daily.mat'
idx=regexp(file,'\.' ,'split')
site=idx{1}
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!