How to parse file name text and save values to variables

15 visualizaciones (últimos 30 días)
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)

Respuesta aceptada

Johnny Himbele
Johnny Himbele el 2 de Nov. de 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

Más respuestas (1)

Sean de Wolski
Sean de Wolski el 2 de Nov. de 2021
Look at various combinations of the following functions
split
contains
matches
extractBetween, extractBefore, extractAfter

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by