How to parse file name text and save values to variables

조회 수: 10 (최근 30일)
Samantha Plesce
Samantha Plesce 2021년 11월 2일
댓글: Samantha Plesce 2021년 11월 4일
I have several file names that I want to parse through to find if they contain certain words.
ex1) HW_Azcut_y-plane_100-500Hz_V-Vp
ex2) XPOLcut_0.5-300MHz_Hp
I would like to be able to store the upper and lower values of frequencies into separate variables, find whether it is in Vertical (V) or Horizontal (H) , the cut type (Az, XPOL)

채택된 답변

Johnny Himbele
Johnny Himbele 2021년 11월 2일
str = 'HW_Azcut_y-plane_100-500MHz_V-Vp';
strPart = split(str,"_");
idxCut = contains(strPart,'cut');
strCut = erase(strPart(idxCut),'cut');
if strcmpi(strCut,'Az')
cutType = 'Az';
elseif strcmpi(strCut,'XPOL')
cutType = 'XPOL';
end
idxFreq = contains(strPart,'Hz');
factor = 1.0;
strFreq = strPart(idxFreq);
if contains(strFreq,'kHz')
factor = 1.0e3;
strErase = 'kHz';
elseif contains(strFreq,'MHz')
factor = 1.0e6;
strErase = 'MHz';
elseif contains(strFreq,'GHz')
factor = 1.0e9;
strErase = 'GHz';
end
strFreq = erase(strFreq,strErase);
strFreq = split(strFreq,'-');
for i = 1:length(strFreq)
freq(i) = str2double(strFreq(i))*factor;
end

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2021년 11월 2일
Look at various combinations of the following functions
split
contains
matches
extractBetween, extractBefore, extractAfter

카테고리

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