Extract Variable from Filename?

조회 수: 1 (최근 30일)
Kate
Kate 2013년 5월 29일
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!

채택된 답변

Matt Kindig
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
Kate
Kate 2013년 5월 29일
Great idea, thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
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}
  댓글 수: 1
Kate
Kate 2013년 5월 29일
I'll try this, thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by