How to parse file name text and save values to variables
11 views (last 30 days)
Show older comments
Samantha Plesce
on 2 Nov 2021
Commented: Samantha Plesce
on 4 Nov 2021
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)
0 Comments
Accepted Answer
Johnny Himbele
on 2 Nov 2021
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
0 Comments
More Answers (1)
Sean de Wolski
on 2 Nov 2021
Look at various combinations of the following functions
split
contains
matches
extractBetween, extractBefore, extractAfter
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!