필터 지우기
필터 지우기

How to add text from name of .txt-file into string in a cell?

조회 수: 1 (최근 30일)
Linus Dock
Linus Dock 2016년 10월 6일
댓글: Linus Dock 2016년 10월 7일
Hi! I wonder if it is possible to add the name of a textfile for example: 200807.txt into strings in a cell that looks like this:
'METAR ESDF 010020Z AUTO 31003KT 230V350 9999NDV NCD 12/08 Q1017'
'METAR ESDF 010050Z AUTO 24003KT 230V320 9999NDV NCD 12/08 Q1017'
'METAR ESDF 010120Z AUTO 25003KT 230V350 9999NDV NCD 11/08 Q1017'
The strings exists in a .txt-file containing 45000 rows approximately that is imported with fread(). The only information about the year and month of the data is from the filename itself. The problem is that I would like to add the name of the file which contains the information about year and month to the part '010020Z'. That would provide a unique date and time to my strings that I could later use with datevec or datenum. Is there a way of doing this with matlab?
Thankful of your help!
  댓글 수: 2
Jim Hokanson
Jim Hokanson 2016년 10월 6일
It would be helpful to provide an example of what your output should look like.
Linus Dock
Linus Dock 2016년 10월 6일
It should look something like this:
'METAR ESDF 200807010120Z AUTO 25003KT 230V350 9999NDV NCD 11/08 Q1017'

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

채택된 답변

Guillaume
Guillaume 2016년 10월 6일
regexprep(yourcellarray, 'd{6}Z', sprintf('%s$0', stringtoadd))
The above regular expression matches exactly 6 digits followed by Z and prepend that section with whichever string you want to add.
  댓글 수: 1
Linus Dock
Linus Dock 2016년 10월 7일
Thank you! This is how it worked out.
flygplatsmetarYMDHM=cell(1,length(txtFiles))
filenamestr={txtFiles.name};
YYYYMM = regexprep(filenamestr, '.txt','')
for n=1:length(txtFiles)
flygplatsYYYYMM=Utcell{n};
yearmonth = YYYYMM{n}
flygplatsmetarYMDHM{n}=regexprep(flygplatsYYYYMM, '\d{6}Z', sprintf('%s$0', yearmonth))
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by